Nginx do nodejs application load Balancer Configuration instance

Source: Internet
Author: User
Tags nginx server

This article mainly introduced the Nginx to do nodejs application load Balancer Configuration example, this article directly gives the configuration instance, needs the friend can refer to.

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, Configuration Nginx modified Nginx.conf:upstream sample {      server 127.0.0.1:3000;       Server 127.0.0.1:3001;      keepalive 64;    }      server {& nbsp;      Listen 80;       ....       server_name 127.0.0.1;&nbsp ;      ....       location/{         Proxy_redirect Off;&nbs p;        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 "";       &NB Sp 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 Servervar http = require (' http ');   var Morgan = require (' Morgan ');  var server1 = Http.createserver (function (req, res) {Console.log ("Request for:" + Req.url + "--port 3000");  Res.writehead ($, {' 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 ($, {' 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 the http://127.0.0.1 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 Reque St for:/favicon.ico--Port 3001 Request for:/--port, Request for:/favicon.ico--Port 3001 Request for:/favicon . ico--Port 3000

Turn from (http://www.jb51.net/article/60524.htm)

Nginx do nodejs application load Balancer Configuration instance

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.