Nginx Load Balancer Configuration Example detailed

Source: Internet
Author: User
Tags nginx server

Load Balancing
Load balancing is our big traffic site to do a thing, let me introduce you to the Nginx server load Balancing configuration method, I hope to have the necessary students help OH.
Let's start with a quick look at what is load balancing, which is interpreted literally to explain the average load sharing of n servers, not because a server is under-loaded and idle on a single server. Then the premise of load balancing is to have more than one server to achieve, that is, more than two units can be.

category of Load Balancing
Polling-application turns to respond to requests (Nginx defaults)
Minimum connection-The request is assigned to the server with the least active connection
Ip-hash-a hash function that determines which server responds to a user's request (based on the client's request IP)

preparing the test environment
Because there is no server, this test directly host the domain name, and then installed in VMware three CentOS.
Test Domain name: a.com
A server ip:192.168.5.149 (master)
b Server ip:192.168.5.27
C Server ip:192.168.5.126

Deployment Ideas
A server as the primary server, the domain name directly resolves to a server (192.168.5.149), by a server load balancer to B server (192.168.5.27) and C server (192.168.5.126).

Domain Name Resolution
Because it is not the real environment, the domain name will use a a.com as a test, so a.com resolution can only be set in the Hosts file.
Open: C:windowssystem32driversetchosts
Add at the end
192.168.5.149 a.com
Save exit and then start command mode ping to see if it has been set successfully

a server nginx.conf settings
Open nginx.conf, and the file location is in the Conf directory of the Nginx installation directory.
Add the following code to the HTTP segment
Upstream A.com {
Server 192.168.5.126:80;
Server 192.168.5.27:80;
}

server{
Listen 80;
server_name a.com;
Location/{
Proxy_pass http://a.com;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
}

Save Restart Nginx

B, C server nginx.conf settings
Open Nginx.confi and add the following code to the HTTP segment
server{
Listen 80;
server_name a.com;
Index index.html;
root/data0/htdocs/www;
}

Save Restart Nginx

Test:
When accessing a.com, in order to distinguish between which server to handle I write a different content under the B, C server index.html file, to make a distinction.
Open the browser to access the A.com results, the refresh will find all the requests are allocated to the host server (192.168.5.149) on the B server (192.168.5.27) and C server (192.168.5.126), the load balancing effect.

-------------------------------------------------------------------------------------------------Advanced---------------------- --------------------------------------------------------------------------- what are the rules for these requests to be distributed? In the above load balancing effect, it seems that all requests are distributed in a specific order (first: 192.168.5.126, Second: 192.168.5.27, Third: 192.168.5.126, fourth time: 192.168.5.27 ... Since we have not previously configured a configuration related to content distribution, Nginx is the default method of polling. Nginx provides 3 in the way, polling is one of the articles at the beginning of the 3 types of instructions, now modify the above code to achieve the other two ways: minimum number of connections: upstream a.com { Least_conn;
Server 192.168.5.126:80;
Server 192.168.5.27:80;
Ip-hash: upstream a.com { Ip_hash;
Server 192.168.5.126:80;
Server 192.168.5.27:80;
What if one of the servers goes down?
When a server goes down, does it affect access?
Let's take a look at the example above, assuming that the C server 192.168.5.126 the machine is down (because it can't emulate the outage, so I shut down the C server) and then come back to see it.
We found that although the C server (192.168.5.126) was down, it did not affect website access. This way, there is no fear of dragging the entire site in a load-balancing mode because of a machine outage.

What if B.Com also set up load balancing?
Very simple, as with the a.com settings. As follows:
Assuming B.Com's primary server IP is 192.168.5.149, load-balanced on 192.168.5.150 and 192.168.5.151 machines
The domain name B.Com is now resolved to 192.168.5.149IP.
Add the following code to the nginx.conf of the primary server (192.168.5.149):
Upstream B.Com {
Server 192.168.5.150:80;
Server 192.168.5.151:80;
}

server{
Listen 80;
server_name B.Com;
Location/{
Proxy_pass http://b.com;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
}

Save Restart Nginx
Set Nginx on 192.168.5.150 and 192.168.5.151 machine, open nginx.conf Add the following code at the end:

server{
Listen 80;
server_name B.Com;
Index index.html;
root/data0/htdocs/www;
}

Save Restart Nginx
After completing the next steps, you can implement the B.Com load balancer configuration.

does the primary server not provide services?
In the above example, we are all applied to the primary server load balancer to other servers, then the primary server itself can also be added to the server list, so that will not be wasted to take a server purely as a forwarding function, but also participate in the provision of services.
If the above case three servers:
A server ip:192.168.5.149 (master)
b Server ip:192.168.5.27
C Server ip:192.168.5.126
We resolve the domain name to a server, and then forwarded by a server to B server and C server, then a server only do a forwarding function, now we let a server also provide site services.

let's start by analyzing the following two scenarios that can occur if you add a master server to upstream:
1, the primary server forwarded to the other IP, the other IP server normal processing;
2, the primary server forwarded to its own IP, and then into the primary server assigned IP there, if it has been assigned to this machine, it will cause a dead loop.
How to solve this problem? Since port 80 is already being used to monitor the processing of load balancing, it is no longer possible to use the 80 port on the server to process a.com access requests, using a new one. So we add the nginx.conf of the main server to the following code:

server{
Listen 8080;
server_name a.com;
Index index.html;
root/data0/htdocs/www;
}

Restart Nginx, in the browser input a.com:8080 try to see if you can access. Results can be accessed normally

since the normal access, then we can add the main server to upstream, but the port to change, the following code:

Upstream A.com {
Server 192.168.5.126:80;
Server 192.168.5.27:80;
Server 127.0.0.1:8080;
}

Since the main server can be added here IP192.168.5.149 or 127.0.0.1 can both represent access to themselves.
Restart Nginx, and then visit a.com to see if it will be assigned to the primary server.
Ok
The primary server is also able to join the service normally.
finally
One, load balancing is not nginx unique, famous dingding Apache also have, but performance may not be as nginx.
Second, more than one server to provide services, but the domain name only resolves to the primary server, and the real server IP will not be ping can be obtained, add a certain security.
Third, the IP in the upstream is not necessarily the intranet, the external network IP can also. However, the classic case is that a LAN in an IP exposure network, the domain name directly resolved to this IP. The primary server is then forwarded to the intranet server IP.
Four, a server outage, will not affect the normal operation of the site, Nginx will not forward the request to the down IP

Nginx Load Balancer Configuration Example detailed

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.