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

The method of Nginx to configure the load balance of NodeJS Application

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "Nginx to do NodeJS application load balancing configuration method", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "Nginx to do NodeJS application load balancing configuration method"!

Load balancer can allocate users' requests to multiple servers for processing, thus realizing the access support for a large number of users. The architecture of load balancing is shown in the figure:

For complex web applications, it is a matter of course to use nginx for front-end load balancing.

Next, we use nginx to do the load balancing of nodejs applications.

1. Configure nginx

Modify nginx.conf:

Upstream sample {server 127.0.0.1 server 3000; server 127.0.1 VR 3001; keepalive 64;} server {listen 80;.... Server_name 127.0.0.1;. Location / {proxy_redirect off; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header x-forwarded-proto $scheme; proxy_set_header host $http_host; proxy_set_header x-nginx-proxy true; proxy_set_header connection ""; proxy_http_version 1.1 Proxy_pass http://sample;}}

There is a node.js server on port 3000 and a node.js server on port 3001, and both servers are doing the same thing. In the upstream section, two node.js servers are configured. In addition, we also set up proxy_pass to act as a http request proxy.

2. Build a nodejs server

Var http = require ('http'); var morgan = require (' morgan'); var server1 = http.createserver (function (req, res) {console.log ("request for:" + req.url + "- port 3000"); res.writehead (3000, "127.0.0.1") Var server2 = http.createserver (function (req, res) {console.log ("request for:" + req.url + "- port 3001"); res.writehead (200,{ 'content-type':' text/plain'}); res.end ('hello node.js\ n');}) .customers (3001, "127.0.0.1") Server1.once ('listening', function () {console.log (' server running at http://127.0.0.1:3000/');}); server2.once ('listening', function () {console.log (' server running at http://127.0.0.1:3001/');}))

3. Access the nginx server

Now we can visit

You can see the following output:

Server running at http://127.0.0.1:3000/ server running at http://127.0.0.1:3001/ request for: /-- port 3001 request for: / favicon.ico-- port 3000 request for: / favicon.ico-- port 3001 request for: /-- port 3000 request for: / favicon.ico-- port 3001 request for: / favicon.ico-- port 3000 request for: /-- port 3001 request for: / favicon.ico-- port 3000 request for: / favicon.ico-- port 3001 request for: /-- port 3000 request for: / favicon.ico-- port 3001 request for: / favicon.ico-- port 3000 Thank you for your reading The above is the content of "how Nginx does the load balancer configuration of NodeJS applications". After the study of this article, I believe you have a deeper understanding of the method of Nginx load balancer configuration of NodeJS applications, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report