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

Detailed explanation of the method of realizing reverse proxy Node.js by using Nginx

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

Share

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

Preface

The company has a project front end that uses node.js for server rendering and then returns it to the browser to solve the SEO problem of a single page. When the project is deployed, use Nginx reverse proxy Node.js. The specific steps are as follows:

(the installation and basic configuration of Nginx and Node.js are skipped directly)

First, we need to open the following configuration in the http node in the nginx.cnf file:

Http {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 / var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include / etc/nginx/mime.types; default_type application/octet-stream # Open the configuration include / etc/nginx/conf.d/*.conf; of this line}

Then the configuration file for each domain name is placed in this directory / etc/nginx/conf.d/, with the file suffix ending with conf.

The first way, this simple:

Server {listen 80; server_name localhost; root / xxx/xxx/hxxydexx/; # set $my_server_name $scheme://$server_name; # if ($my_server_name! = https://$server_name) {# rewrite ^ https://$server_name$request_uri? Permanent; #} error_log / var/log/nginx/hyde_error.log error; access_log / var/log/nginx/hyde_accss.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_http_version 1.1; proxy_set_header Connection "" # without considering the load, there is no need to configure the upstream node. Proxy_pass http://127.0.0.1:3000;} error_page 404 / 404.html; location = / xxx/xxx/40x.html {} error_page 500502503504 / 50x.html; location = / xxx/xxx/50x.html {}

two。 The second way, taking into account the load

Upstream node {server 127.0.0.1 server 3000;} server {listen 80; server_name localhost; root / xxx/xxx/hxxydexx/; # set $my_server_name $scheme://$server_name; # if ($my_server_name! = https://$server_name) {# rewrite ^ https://$server_name$request_uri? Permanent; #} error_log / var/log/nginx/hyde_error.log error; access_log / var/log/nginx/hyde_accss.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_http_version 1.1; proxy_set_header Connection "" # configure upstream node proxy_pass http://node;} error_page 404 / 404.html; location = / xxx/xxx/40x.html {} error_page 500502503504 / 50x.hml; location = / xxx/xxx/50x.html {}}

Then restart or reload the nginx configuration file. The command is as follows:

# check whether the syntax in the nginx configuration file is correct nginx-t # restart the nginxservice nginx restart# reload configuration file nginx-s reload

Pay attention to the problems:

The following problems may arise:

Events.js:72 throw er; / / Unhandled 'error' event ^ Error: listen EADDRINUSE at errnoException (net.js:884:11) at Server._listen2 (net.js:1022:14) at listen (net.js:1044:10) at Server.listen (net.js:1110:5) at Object. (folderName/app.js:33:24) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10)

This is actually an error caused by the occupation of multiple open ports of the Node.js service. If you have this problem, you can use the Node.js project management tool pm2, or use netstat-anop to see that the port is occupied by that process, and then kill the restart service!

Attach the load balancing strategy of Nginx:

Polling (default)

Each request is assigned to a different backend server one by one in chronological order. If the backend server down is dropped, it can be automatically eliminated.

Upstream backserver {server 192.168.0.14; server 192.168.0.15;}

Specify weight

Specify the polling probability. The weight is proportional to the access ratio, which is used in the case of uneven performance of the backend server.

Upstream backserver {server 192.168.0.14 weight=10; server 192.168.0.15 weight=10;}

IP binds ip_hash

Each request is allocated according to the hash result of accessing the ip, so that each visitor accesses a back-end server on a regular basis, which can solve the session problem.

Upstream backserver {ip_hash; server 192.168.0.14 88; server 192.168.0.15 purl 80;}

Fair (third party)

Requests are allocated according to the response time of the back-end server, and priority is given to those with short response time.

Upstream backserver {server 192.168.0.14 fair; 88; server 192.168.0.15 purl 80; fair;}

Url_hash (third party)

Allocate requests according to the hash result of accessing url, so that each url is directed to the same backend server, which is more effective when the backend server is cached.

Upstream backserver {server squid1:3128; server squid2:3128; hash $request_uri; hash_method crc32;}

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

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