Server cluster is our kind of large-scale millions IP operations personnel will operate, today's small also to everyone over an nginx load Balancing configuration cluster method, online to find a few articles, found it is not difficult.
Nginx Load Balancer One,
1. Do not install in the source code in the same directory, make will be error.
code is as follows |
copy code |
./configure--prefix=/opt/nginx make && make install |
2. Modify the configuration file/opt/nginx/conf/nginx.conf
before the server element: (assuming that two Tomcat ports are 8080)
code is as follows |
copy code |
upstream www.111cn.net { server 192.168.1.13:8080; server 192.168.1.14:8080; } |
Change the server_name under the server element to the actual domain name, for example:
The code is as follows |
Copy Code |
server_name www.111cn.net |
Add a row below the location element:
The code is as follows |
Copy Code |
Proxy_pass www.111cn.net |
3. Start Stop command
Start:/opt/nginx/sbin/nginx
Quick Stop:/opt/nginx/sbin/nginx-s stop
Complete ordered stop:/opt/nginx/sbin/nginx-s quit
Reload:/opt/nginx/sbin/nginx-s Reload
Nginx Load Balancer Two,
Let's say we have 3 servers and the IP addresses are:
192.168.0.1/192.168.0.2/192.168.0.3
We use 192.168.0.1 as the front-end primary server, 192.168.0.2 and 192.168.0.3 as a load-balanced back-end server.
Here are 192.168.0.1 configuration methods for the Nginx on the primary server:
The code is as follows |
Copy Code |
Worker_processes 1; Events { Worker_connections 1024; } HTTP { Upstream ServerList { Server 192.168.0.2:8000 weight=3 max_fails=3 fail_timeout=20s; Server 192.168.0.3:8000 weight=7 max_fails=3 fail_timeout=20s; } server { Listen 80; server_name www.111cn.net; Location/{ Proxy_pass http://serverlist; Proxy_set_header HOST $host; Proxy_set_header X-real-ip $remote _addr; Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; } } } |
Configuration Description:
Upstream serverlist{} can be understood as a server pool, through the Proxy_pass http://serverlist command, Requests to access www.111cn.net can be assigned to servers 192.168.0.2 and 192.168.0.3 in the pool.
Weight=3 and weight=7 are the weights for the server, and the higher the weights, the more requests are allocated.
The implication of max_fails=3 and fail_timeout=20s is that the server will be temporarily offline for 20 seconds when the number of failed requests reaches 3 times.
Tip: The primary server can be responsible for static content, assigning all PHP requests to the backend server for processing.
Configuration of Nginx Load balancer