Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Nginx reverse proxy Node.js

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to use Nginx reverse proxy Node.js? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Install pm2

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

Modify 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"}

Start pm2

Npm run pm2

Boot and start pm2

Pm2 savepm2 startup centos

Be careful

If pm2 startup centos fails, try 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

Modify Nginx configuration

Vim / etc/nginx/conf.d/*.confupstream nodejs {server 127.0.0.1 keepalive 3000; keepalive 64;} server {listen 80; server_name domain.cn; root / web/mazey.cn; index index.html index.htm; # go 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 when the website cuts to / server 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;}}

Corresponding 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)}) the answer to the question on how to use Nginx reverse proxy Node.js is shared here I hope the above content can help you to a certain extent, if you still have a lot of doubts to be solved, you can follow the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report