Home Backend Development PHP Tutorial Using Nginx for load balancing of NodeJS applications

Using Nginx for load balancing of NodeJS applications

Aug 08, 2016 am 09:30 AM
http port proxy quot request

"Using Nginx for load balancing of NodeJS applications"

Author: chszs, please indicate when reprinting. Blog homepage: http://blog.csdn.net/chszs

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


For complex web applications, using Nginx for front-end load balancing is a matter of course.

Next, we use Nginx to load balance NodeJS applications.

1. Configure Nginx

Modify nginx.conf:

....
        upstream sample {
	      server 127.0.0.1:3000;
	      server 127.0.0.1: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;
           }
        }
Copy after login
There is a Node.js server on port 3000 and port 3001. These two servers are doing the same job. In the upstream section, two Node.js servers are configured. In addition, we also set proxy_pass http://sample as HTTP request proxy.

2. Build 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(200, {'Content-Type': 'text/plain'});
  res.end('Hello Node.js\n');
}).listen(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');
}).listen(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/');
});
Copy after login
3. Access Nginx server

Now we can access http://127.0.0.1

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 
Copy after login

The above introduces the use of Nginx for load balancing of NodeJS applications, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does http status code 520 mean? What does http status code 520 mean? Oct 13, 2023 pm 03:11 PM

HTTP status code 520 means that the server encountered an unknown error while processing the request and cannot provide more specific information. Used to indicate that an unknown error occurred when the server was processing the request, which may be caused by server configuration problems, network problems, or other unknown reasons. This is usually caused by server configuration issues, network issues, server overload, or coding errors. If you encounter a status code 520 error, it is best to contact the website administrator or technical support team for more information and assistance.

Nginx Proxy Manager Tutorial: Quick Start Guide Nginx Proxy Manager Tutorial: Quick Start Guide Sep 27, 2023 pm 05:39 PM

NginxProxyManager Tutorial: Quick Start Guide, Specific Code Examples Needed Introduction: With the development of network technology, proxy servers have become a part of our daily use of the Internet. NginxProxyManager is a proxy server management platform based on Nginx, which can help us quickly establish and manage proxy servers. This article will introduce you to the quick start guide of NginxProxyManager, as well as some specific code examples. one

Deployment strategy of containers and microservices under Nginx Proxy Manager Deployment strategy of containers and microservices under Nginx Proxy Manager Sep 27, 2023 pm 01:06 PM

The deployment strategy of containers and microservices under NginxProxyManager requires specific code examples. Summary: With the popularity of microservice architecture, containerization technology has become an important part of modern software development. In the microservice architecture, NginxProxyManager plays a very important role, used to manage and proxy the traffic of microservices. This article will introduce how to use NginxProxyManager to deploy and manage containerized microservices, and provide relevant code examples.

How to use Nginx Proxy Manager to achieve load balancing of multiple servers How to use Nginx Proxy Manager to achieve load balancing of multiple servers Sep 27, 2023 pm 09:42 PM

How to use NginxProxyManager to achieve load balancing of multiple servers. NginxProxyManager is a proxy server management tool developed based on Nginx. It provides a simple and easy-to-use Web interface that can easily configure and manage Nginx proxy servers. In practical applications, we often need to distribute requests to multiple servers to achieve load balancing and improve system performance and availability. This article will introduce how to use NginxProx

What is http status code 403? What is http status code 403? Oct 07, 2023 pm 02:04 PM

HTTP status code 403 means that the server rejected the client's request. The solution to http status code 403 is: 1. Check the authentication credentials. If the server requires authentication, ensure that the correct credentials are provided; 2. Check the IP address restrictions. If the server has restricted the IP address, ensure that the client's IP address is restricted. Whitelisted or not blacklisted; 3. Check the file permission settings. If the 403 status code is related to the permission settings of the file or directory, ensure that the client has sufficient permissions to access these files or directories, etc.

Understand common application scenarios of web page redirection and understand the HTTP 301 status code Understand common application scenarios of web page redirection and understand the HTTP 301 status code Feb 18, 2024 pm 08:41 PM

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

How to use Nginx Proxy Manager to quickly cache HTML pages How to use Nginx Proxy Manager to quickly cache HTML pages Sep 28, 2023 am 10:58 AM

How to use NginxProxyManager to achieve fast caching of HTML pages Introduction: In modern network applications, fast loading of web pages is an important requirement. In order to improve user experience, we can use NginxProxyManager to quickly cache HTML pages. This article will show you how to use NginxProxyManager to achieve this goal, and provide specific code examples. Part 1: Install and configure NginxProxy

http request 415 error solution http request 415 error solution Nov 14, 2023 am 10:49 AM

Solution: 1. Check the Content-Type in the request header; 2. Check the data format in the request body; 3. Use the appropriate encoding format; 4. Use the appropriate request method; 5. Check the server-side support.

See all articles