Nginx Load Balancing Configuration Scenario
Nginx
The proxy_pass
proxy module is used to implement load balancing Core configuration, forward the client request proxy to a set of upstream
virtual service pool
Nginx upstream
virtual configuration syntax
Syntax: upstream name {... }Default: -Context: http//upstream example upstream backend {server backend1.example.com weight=5; server backend2.example.com:8080; server unix:/tmp /backend3; server backup1.example.com:8080 backup;}server {location / {proxy_pass http://backend; }}
1. Create the corresponding html
file p>
[[emailprotected] ~]# mkdir /soft/{code1,code2,code3} -p[[emailprotected] ~]# cat /soft/code1/index.htmlCode1 Code1-8081
[[emailprotected] ~]# cat /soft/code2/index .htmlCoder2 Code1-8082
[[emailprotected] ~]# cat /soft/code3/index.htmlCoder3 Code1-8083
2. Create the corresponding releserver.conf
configuration file
[[emailprotected] ~]# cat /etc/nginx/conf.d/ releserver.conf server {listen 8081; root /soft/code1; index index.html;}server {listen 8082; root /soft/code2; index index.html;}server {listen 8083; root /soft/code3; index index .html;}
3. Configure Nginx
reverse proxy
[[emailprotected] ~]# cat /etc/nginx /conf.d/proxy.conf upstream node {server 192.168.69.113:8081; server 192.168.69.113:8082; server 192.168.69.113:8083;}server {server_name 192.168.69.113; listen 80; location / {proxy_pass http:/ /node; include proxy_params; }}
Nginx load balancing state configuration
The state of the backend server in load balancing scheduling
Status | Overview |
---|---|
down | The current server is temporarily not involved in load balancing |
backup | Reserved backup server |
max_fails | The number of allowed request failures |
fail_timeout | After max_fails fails, the service suspension time |
max_conns | Limit the maximum number of received connections |
Test backup
and down
Status
upstream load_pass {server 192.168.56.11:8001 down; server 192.168.56.12:8002 backup; server 192.168.56.13:8003 max_fails=1 fail_timeout=10s;}location / { proxy_pass http://load_pass; include proxy_params;}//Close 8003 test
Nginx load balancing scheduling strategy
Scheduling Algorithm | Overview |
---|---|
Polling | Assign to different back-end servers one by one in chronological order (default) |
weight | weighted polling, weight value The larger the assigned access The higher the probability |
ip_hash | Each request is allocated according to the hash result of the access IP, so that a fixed visit from the same IP End server |
url_hash | According to the hash result of the access URL, the request is distributed, and each URL is directed to the same back-end server |
least_conn | Minimum number of links, distribute the machine with less links |
hash key value | hash custom key |
Nginx load balancing weight polling specific configuration< /p>
upstream load_pass {server 192.168.56.11:8001; server 192.168.56.12:8002 weight=5; server 192.168.56.13:8003;}
Nginx load balancing< code>ip_hash Specific configuration
//If the clients are all using the same proxy, it will cause too many connections to a certain server upstream load_pass {ip_hash; server 192.168.56.11: 8001; server 192.168.56.12:8002; server 192.168.56.13:8003;}//If access through a proxy occurs, it will affect the back-end node receiving state balance
Nginx load balancing url_hash specific configuration
< pre class="nginx">upstream load_pass {hash $request_uri; server 192.168.56.11:8001; server 192.168.56.12:8002; server 192.168.56.13:8003;}//Add the same file for three servers/s oft/code1/url1.html url2.html url3.html/soft/code2/url1.html url2.html url3.html/soft/code3/url1.html url2.html url3.html
Nginx load balancing TCP configuration
Nginx
The four-layer proxy can only exist in the main
section
stream {upstream ssh_proxy {hash $remote_addr consistent; server 192.168.56.103:22;} upstream mysql_proxy {hash $remote_addr consistent; server 192.168.56.103:3306;} server {listen 6666; proxy_connect_timeout 1s; proxy_timeout 300s; proxy_pass ssh_proxy;} server {listen 5555; proxy_connect_timeout 1s; proxy_timeout 300s; proxy_pass mysql_proxy; }}