Load balancing for NODEJS applications with Nginx

Source: Internet
Author: User
Tags php tutorial nginx server
"Load balancing using Nginx for Nodejs applications"

Author: Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs

Load balancing allows the user's requests to be distributed across multiple servers for processing, enabling access to a huge number of users. Load-Balanced Architecture:


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

Below, we use Nginx to do the load balance of NODEJS application.

1. Configure Nginx

Modify Nginx.conf:

....        Upstream Sample {      server 127.0.0.1:3000;      Server 127.0.0.1:3001;      keepalive;    }         server {        listen;        ....            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 each of the 3000 and 3001 ports, and the two servers are doing the same job. In the upstream section, two node. JS servers are configured. In addition, we have set up Proxy_pass http://sample to do HTTP request proxy.

2. Build Nodejs Server

var http = require (' http '); var morgan       = require (' Morgan '); var server1 = Http.createserver (function (req, res) {  C Onsole.log ("Request for:  " + Req.url + "-port");  Res.writehead ($, {' Content-type ': ' Text/plain '});  Res.end (' Hello node.js\n ');}). Listen ("127.0.0.1"), var server2 = Http.createserver (function (req, res) {  console.log ("Request for:  " + Req.url + "--port 3001");  Res.writehead ($, {' 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/');});
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 @ http://127.0.0.1:3001/  Request for:  /--Port 3001   Request for:  /favicon.ico--Port,   request for:  /favicon.ico--, Port 3001   request for:  /--Port   Request for:  /favicon.ico--Port 3001   request for:  /favicon.ico--Port,   request for:  /-- Port 3001   Request for:  /favicon.ico--Port,   request for:  /favicon.ico--Port 3001   Request for:  /--Port, request for   :  /favicon.ico--Port 3001   Request for:  

The above describes the use of Nginx to do nodejs application load balancing, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.