安装pm2

npm install pm2 -g

ln -s /home/download/node-v8.11.1-linux-x64/lib/node_modules/pm2/bin/pm2 /usr/local/bin/pm2

修改package.json

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"pm2": "/home/download/node-v8.11.1-linux-x64/lib/node_modules/pm2/bin/pm2 start /web/mazey.cn/server/app.js"
} or "scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"pm2": "pm2 start app.js"
}

启动pm2

npm run pm2

开机启动pm2

pm2 save

pm2 startup centos

注意

pm2 startup centos失败,可尝试pm2 startup

 PM2 detected systemv but you precised centos
Please verify that your choice is indeed your init system
If you arent sure, just run : pm2 startup

修改Nginx配置

vim /etc/nginx/conf.d/*.conf

upstream nodejs {
server 127.0.0.1:3000;
keepalive 64;
}
server {
listen 80;
server_name domain.cn;
root /web/mazey.cn;
index index.html index.htm;
# 网站切到/server下时走nodejs
location /server {
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_pass http://nodejs;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 1h;
}
}

相应的 app.js

const express = require('express')
const app = express()
let hi = 'hi' app.get('/server', (req, res, next) => {
hi = `Hello Mazey!\n`
next()
}, (req, res) => {
res.send(`
${hi}
${req.method}\n
${req.originalUrl}\n
${req.query.id}\n
`)
}) const server = app.listen(3000, function () {
let host = server.address().address
let port = server.address().port console.log('Example app listening at http://%s:%s', host, port)
})

注意

若报错 Cannot GET /xxx 说明 Express 的路由没配好。

用Nginx反向代理Node.js的更多相关文章

  1. nginx反向代理node.js获取客户端IP

    使用Nginx做node.js程序的反向代理,会有这么一个问题:在程序中获取的客户端IP永远是127.0.0.1 如果想要拿到真实的客户端IP改怎么办呢? 一.首先配置Nginx的反向代理 proxy ...

  2. Nginx反向代理node,实现让静态文件在同一域

    Nginx反向代理node,实现让静态文件在同一域 原文https://github.com/zhuangZhou/Blog/issues/4 不管是Vue还是React,还是传统的网站,与node服 ...

  3. apache2反向代理node.js应用

    在之前记录的随笔中,只是介绍了怎么在apache2中使用proxy模块,后来查到了一些资料,可以通过下面网址查看配置块的详细参数信息 http://man.ddvip.com/soft/apache2 ...

  4. CentOS 7 安装 Nginx 反向代理 node

    安装 nginx yum install epel-release yum install nginx 配置 nginx sudo vim /etc/nginx/nginx.conf, 改成下面配置: ...

  5. node项目发布+域名及其二级域名配置+nginx反向代理+pm2

    学习node的时候也写了一些demo.但是只是限于本地测试,从来没有发布.今天尝试发布项目. 需要准备的东西 node 项目:为了突出重点,说明主要问题.我只是拿express 写了很简单的demo. ...

  6. Nginx反向代理以及负载均衡配置

    项目地址:http://git.oschina.net/miki-long/nginx 前提:最近在研究nginx的用法,在windows上小试了一下,由于windows下不支持nginx缓存配置,所 ...

  7. 【转】Nginx反向代理和负载均衡

    原文链接:http://www.cnblogs.com/shuoer/p/7820899.html Nginx反向代理和负载均衡 环境说明 由于我使用的是windows系统,所以我用虚拟机虚拟出来了3 ...

  8. 跨域问题,解决方案-Nginx反向代理

    跨域问题,解决之道 跨域问题,在日常开发过程中,是一个非常熟悉的名词.今天的话题,结合我之前的项目场景,讨论下<跨域问题,解决之道>. 跨域是什么 跨域问题,是由于JavaScript出于 ...

  9. 14 微服务电商【黑马乐优商城】:day06-使用nginx反向代理并掌握cors解决跨域

    本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一) ...

随机推荐

  1. 每日英语:China Targets Big Pharma

    China unveiled a litany of bribery and misconduct allegations against GlaxoSmithKline GSK.LN -0.26% ...

  2. vue的路由使用

    1). 安装 vue-router npm install vue-router --save 2). 新建路由配置 安装成功后,在 src 新建 router 文件夹,然后新建 index.js 文 ...

  3. 用log4j将日志写入数据库

    以下为log4j中的配置参数: %m 输出代码中指定的消息 %p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL %r 输出自应用启动到输出该log信息耗费的毫秒数 %t 输出产 ...

  4. 应有dataGridView控件

    using System.Data.SqlClient; namespace UseDataGridView { public partial class Form1 : Form { public ...

  5. Android——Android Studio的一些小技巧(转)

    ndroid课程---Android Studio的一些小技巧   APK瘦身 在Android Studio中我们可以开启混淆,和自动删除没有Resources文件,来达到给APP瘦身的目的,这对于 ...

  6. 关于Aspose强大的应用--EXECL

    protected void btnConfirg_Click(object sender, EventArgs e) { genExcel(); } //设置内容文字色 表中有一个蓝色文字列和绿色文 ...

  7. [velocity] velocity详解

    (1)为什么要使用模版语言? 在服务器端可以使用 Velocity 处理模板和生成的动态内容(HTML.XML等).这和 JSP 技术的目标非常接近.但是,JSP 模型可以毫无阻碍地访问底层的 Ser ...

  8. Try中如果发现错误,即跳出try去匹配catch,那么try后面的语句就不会被执行

    例:public void print() throws Exception. 对于方法a,如果它定义了throws Exception.那么当它调用的方法b返回异常对象时,方法a并不处理,而将这个异 ...

  9. XML基础知识-->Spring配置

    XML的特殊字符 XML中共有5个特殊的字符,分别是:&<>“’.如果配置文件中的注入值包括这些特殊字符,就需要进行特别处理.有两种解决方法:其一,采用本例中的<![CDAT ...

  10. 【转】关于OnPaint的工作机制

    转载出处:http://blog.csdn.net/foreverhuylee/article/details/21889025 用了两年的VC++,其实对OnPaint的工作原理一直都是一知半解.这 ...