当前位置: 首页 > news >正文

阿里云ubuntu14.04 Nginx反向代理Nodejs

Install Nginx

0.通过ssh命令行登陆阿里云服务器

ssh root@your_ip_address

1.下载源码包

 wget http://nginx.org/download/nginx-1.9.10.tar.gz

2.进入下载路径

$ ./configure
$ make
$ make install

其中configure文件是Nginx软件的自动脚本程序。运行configure自动脚本会完成两项工作:

一是检查环境,根据环境检查结果生成C代码;
二是生成编译代码需要的Makefile文件。

Start Nginx

安装后程序在usr/local下面,执行

$ ./sbin/nginx

查看是否启动成功

可以使用Linux自带的ps工具

ps -ef | grep nginx
root     14358     1  0 11:20 ?        00:00:00 nginx: master process ./sbin/nginx
nobody   14359 14358  0 11:20 ?        00:00:00 nginx: worker process
nobody   14360 14358  0 11:20 ?        00:00:00 nginx: worker process
nobody   14361 14358  0 11:20 ?        00:00:00 nginx: worker process
root     14433 14278  0 12:32 pts/1    00:00:00 grep --color=auto nginx

显示启动成功,包括一个主线程和三个worker线程。

配置nginx.conf

下面是整个nginx.conf配置文件

#user  nobody;
worker_processes  3;
.
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream nodejs__upstream {
        server 127.0.0.1:3000;
        eepalive 64;
    }
    server {
        listen       80;
        server_name  jianqunzhang.com www.jianqunzhang.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
               proxy_set_header   X-Real-IP            $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_set_header   Host                   $http_host;
                proxy_set_header   X-NginX-Proxy    true;
                proxy_set_header   Connection "";
                proxy_http_version 1.1;
                proxy_pass         http://nodejs__upstream;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

其中主要增加修改如下配置

upstream nodejs__upstream {
    server 127.0.0.1:3000;
    keepalive 64;
}
server {
    listen       80;
    server_name  jianqunzhang.com www.jianqunzhang.com;
    
    #charset koi8-r;
    
    #access_log  logs/host.access.log  main;
    
    location / {
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   Host $http_host;
            proxy_set_header   X-NginX-Proxy    true;
            proxy_set_header   Connection "";
            proxy_http_version 1.1;
            proxy_pass         http://nodejs__upstream;
}

其中proxy_set_header指令可以更改Nginx服务器接收到的客户端请求的请求头信息,然后将新的请求头发送给被代理的服务器。

在服务器启动nodejs,注意监听的端口与配置的nginx.conf要一致。本配置直接监听3000端口。
如果proxy_pass http://nodejs__upstream;在不设置http://时,要注意

upstream nodejs__upstream {
    server 127.0.0.1:3000;
    keepalive 64;
}

的server 要加上http://,如

upstream nodejs__upstream {
    server http://127.0.0.1:3000;
    keepalive 64;
}

启动nodejs后,访问所设置的域名即可。

Install PM2

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

npm i pm2 -g

start app

pm2 start bin/www

到目前为止,整个项目算是跑起来了。记录一下同时也希望对小伙伴们有所帮助。

相关文章:

  • vSphere中CPU的设置选择及运行流程
  • iOS 系统状态栏 隐藏 或 显示
  • 页面常见效果js实现
  • Java GBK中文乱码问题分析
  • 巧用位运算求解二进制中1的个数
  • Linux Netcat 命令——网络工具中的瑞士军刀
  • python学习笔记-Day14 -js/dom/jquery
  • C++构造函数详解及显式调用构造函数
  • web应用中log4j应用--log4j.xml同时输出多种日志
  • apache的动态和静态
  • Navicat 结构同步常规设置讲解
  • hbase参考官方文档搭建完全分布式环境遇到的问题
  • 【设计模式】GoF设计模式学习总结
  • 毛毛雨的博客乐园—内容简介
  • Android布局— — —表格布局
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • Django 博客开发教程 16 - 统计文章阅读量
  • es的写入过程
  • fetch 从初识到应用
  • spring + angular 实现导出excel
  • webgl (原生)基础入门指南【一】
  • 机器学习学习笔记一
  • 今年的LC3大会没了?
  • 排序(1):冒泡排序
  • 通过npm或yarn自动生成vue组件
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • 2017年360最后一道编程题
  • PostgreSQL之连接数修改
  • RDS-Mysql 物理备份恢复到本地数据库上
  • 完善智慧办公建设,小熊U租获京东数千万元A+轮融资 ...
  • ​【已解决】npm install​卡主不动的情况
  • #NOIP 2014# day.2 T2 寻找道路
  • #QT(一种朴素的计算器实现方法)
  • (2022 CVPR) Unbiased Teacher v2
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (备忘)Java Map 遍历
  • (超详细)语音信号处理之特征提取
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (非本人原创)史记·柴静列传(r4笔记第65天)
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (接口自动化)Python3操作MySQL数据库
  • (一)UDP基本编程步骤
  • .NET Entity FrameWork 总结 ,在项目中用处个人感觉不大。适合初级用用,不涉及到与数据库通信。
  • .Net 高效开发之不可错过的实用工具
  • .net 提取注释生成API文档 帮助文档
  • .stream().map与.stream().flatMap的使用
  • /var/lib/dpkg/lock 锁定问题
  • @Controller和@RestController的区别?
  • @hook扩展分析
  • @SpringBootApplication 包含的三个注解及其含义
  • [2016.7.test1] T2 偷天换日 [codevs 1163 访问艺术馆(类似)]
  • [20190113]四校联考
  • [④ADRV902x]: Digital Filter Configuration(发射端)
  • [acm算法学习] 后缀数组SA