Nginx load balancing,
Test Environment
In this test, the host directly specifies the domain name, and then three CentOS instances are installed on the virtual machine.
Test Domain Name: a.com
Server a ip Address: 192.168.0.108 (master)
Server B IP Address: 192.168.0.27
IP address of the C server: 192.168.0.131
Deployment ideas
Server A serves as the master server, and the domain name is directly resolved to server A (192.168.5.149). server A performs load balancing on server B (192.168.0.27) and server C (192.168.0.131.
Domain name resolution
Because it is not a real environment, the domain name can use a.com for testing. Therefore, the resolution of a.com can only be set in the hosts file.
Open: C: WindowsSystem32driversetchosts
Add at the end
192.168.0.108 a.com
Save and exit, and then start command mode ping to see if the setting is successful
From the above, a.com is successfully resolved to 192.168.5.149IP.
Nginx. conf settings of server
Open nginx. conf. The file is located in the conf directory of the nginx installation directory.
Add the following code to the http segment:
Upstream a.com {
Server 192.168.0.131: 80;
Server 192.168.0.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 and restart nginx
B. Configure nginx. conf on the C Server
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 and restart nginx
Test
At the.comtime, I wrote a different index.html file under the B 、c server to differentiate the server handler.
Open the browser to access a.com and refresh the results. All requests are allocated to server B (192.168.0.27) and server C (192.168.0.131) by the master server (192.168.5.149, load Balancing is achieved.
Server B processing page
Server C processing page
What if one of the servers goes down?
When a server goes down, will access be affected?
Let's take a look at the instance. Based on the above example, assume that the host 192.168.0.131 of the c server is down (because it cannot be simulated, I will shut down the C server) and then visit it.
Access results:
We found that although server C (192.168.0.131) is down, website access is not affected. In this way, the whole site will not be dragged down due to the downtime of a server in Server Load balancer mode.
What should I do if I want to configure Server Load balancer for B .com?
It's easy, just like setting a.com. As follows:
Assume that the master server IP address of B .com is 192.168.5.149, And the Server Load balancer is distributed to machines 192.168.5.150 and 192.168.5.151.
Resolve the domain name B .com to 192.168.5.149IP.
Add the following code to nginx. conf of the master 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 and restart nginx
Set nginx on machines 192.168.5.150 and 192.168.5.151, enable nginx. conf, and add the following code at the end:
Server {
Listen 80;
Server_name B .com;
Index index.html;
Root/data0/htdocs/www;
}
Save and restart nginx
After completing the subsequent steps, you can configure B .com Server Load balancer.
Does the master server provide services?
In the preceding example, the Server Load balancer of the master server is applied to other servers. Can the master server itself be added to the server list, in this way, a server is not wasted as a forwarding function, but also involved in the provision of services.
For example, in the above case, three servers:
Server a ip Address: 192.168.0.108 (master)
Server B IP Address: 192.168.0.27
IP address of the C server: 192.168.0.131
We resolve the domain name to server A, and then forward the domain name to server B and server C by server A. Then server A only provides A forwarding function. Now we have server A provide site services.
Let's analyze it first. If you add the master server to upstream, there may be two situations:
1. The master server is forwarded to other IP addresses, and other IP addresses are processed properly;
2. The master server forwards the IP address to the master server and then allocates the IP address to the master server. If the IP address is always allocated to the local server, an endless loop will occur.
How can this problem be solved? Because port 80 is already used to listen for Server Load balancer processing, the server cannot use port 80 to process a.com access requests. A new one is required. Therefore, we add the nginx. conf of the master server to the following code:
Server {
Listen 8080;
Server_name a.com;
Index index.html;
Root/data0/htdocs/www;
}
Restart nginx and enter a.com: 8080 in the browser to see if it can be accessed. The result can be accessed normally.
Since normal access is available, we can add the master server to upstream, but the port should be changed as follows:
Upstream a.com {
Server 192.168.0.131: 80;
Server 192.168.0.27: 80;
Server 127.0.0.1: 8080;
}
You can add the master server IP192.168.0.108 or 127.0.0.1 to access your own IP address.
Restart Nginx, and then visit a.com to see if it will be allocated to the master server.