nginx反向代理node.js应用

安装nginx

tar xf nginx-1.13.9.tar.gz

 cd nginx-1.13.9/

安装依赖包

 yum install gcc gcc-c++ pcre* zlib-devel openssl-devel gd-devel  php php-mysql php-fpm make -y

编译 安装 nginx

./configure --prefix=/usr/local/nginx --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_image_filter_module --with-http_slice_module --with-mail --with-threads --with-file-aio --with-stream --with-mail_ssl_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-pcre  --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module  && make && make install

启动并查看

/usr/local/nginx/sbin/nginx 

ps aux | grep nginx

源码包安装Node.js

Pm2   https://www.jianshu.com/p/43525232b03b

 tar xf node-v8.11.3.tar.gz 

cd node-v8.11.3

安装在/usr/local/node

编译安装

./configure --prefix=/usr/local/node && make && make install

检测是否安装成功

加入环境变量

 vi /etc/profile.d/node.sh

export PATH=$PATH:/usr/local/node/bin

source /etc/profile.d/node.sh 

[root@node03 conf]# node -v

v8.11.3

[root@localhost www]# npm -v

5.6.0

配置nginx反向代理node.js

   server {

        listen       80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

         location / {

        proxy_pass http://127.0.0.1:3000;

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header Connection upgrade;

        proxy_set_header Host $host;

        proxy_cache_bypass $http_upgrade;

       }

检查配置文件  没问题后重启nginx

[root@localhost yum.repos.d]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

/usr/local/nginx/sbin/nginx -s reload

安装pm2            pm2是启动node.js用的

npm install -g pm2

编辑helloworld.js 文件

var http = require('http');

http.createServer(function (req, res) {

    res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World ');

}).listen(1337, "127.0.0.1");

console.log('Server running at http://127.0.0.1:1337/');

启动node.js项目  

pm2 start helloworld.js --name 'helloworld'

查看pm2的进程和项目

 pm2 monit

把服务加入系统中,再保存一下

 pm2 startup centos

  pm2 save

###########下面方式不可用  关闭shell以后node.js的进程将会被取消,相当于摁了ctrl+c

上传Node.js项目 在项目下 安装依赖的包和库

 npm install

启动node.js 服务

 npm run serve

启动node.js 客户端

npm run client

原文地址:https://www.cnblogs.com/qingyuanyuanxi/p/9414079.html