国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Nginx > 正文

用Nginx反向代理Node.js的方法

2024-08-30 12:21:48
字體:
供稿:網(wǎng)友
本篇文章主要介紹了用Nginx反向代理Node.js的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了用Nginx反向代理Node.js的方法,分享給大家,具體如下:

安裝pm2

npm install pm2 -gln -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 savepm2 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/*.confupstream 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;  # 網(wǎng)站切到/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;  }}

相應(yīng)的 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 的路由沒配好。

武林網(wǎng)之家提示您:這篇文章你看完了!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 南乐县| 宾川县| 晴隆县| 中西区| 高雄县| 宁远县| 绩溪县| 全椒县| 吴江市| 荃湾区| 平乐县| 云霄县| 保定市| 曲靖市| 临沂市| 神农架林区| 科技| 平谷区| 新丰县| 公主岭市| 西平县| 扬中市| 康乐县| 新昌县| 太湖县| 新邵县| 安新县| 林甸县| 海南省| 弥渡县| 乌苏市| 桂平市| 襄城县| 阜宁县| 凌海市| 收藏| 阆中市| 宁德市| 陆良县| 萨嘎县| 苏尼特左旗|