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

Install Nginx

0.經過ssh命令行登錄阿里雲服務器php

ssh root@your_ip_address

1.下載源碼包html

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

2.進入下載路徑node

$ ./configure
$ make
$ make install

其中configure文件是Nginx軟件的自動腳本程序。運行configure自動腳本會完成兩項工做:nginx

一是檢查環境,根據環境檢查結果生成C代碼;
二是生成編譯代碼須要的Makefile文件。npm

Start Nginx

安裝後程序在usr/local下面,執行服務器

$ ./sbin/nginx

查看是否啓動成功

能夠使用Linux自帶的ps工具session

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線程。app

配置nginx.conf

下面是整個nginx.conf配置文件ssh

#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;
    #    }
    #}

}

其中主要增長修改以下配置tcp

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

到目前爲止,整個項目算是跑起來了。記錄一下同時也但願對小夥伴們有所幫助。

相關文章
相關標籤/搜索