实现请求负载均衡
配置多个目标服务器,当一台服务器出现故障时,nginx
能将请求自动转向另一台服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| http { sendfile_max_chunk 100k; keepalive_timeout 65;
upstream mysvr { server 127.0.0.1:7878 weight=2; server 192.168.10.121:3333 backup weight=1; } error_page 404 https://www.baidu.com; server { keepalive_requests 120; listen 9090; server_name 127.0.0.1; location ~*^.+$ { proxy_pass http://mysvr; deny 127.0.0.1; allow 172.18.5.54; index index.php index.html; rewrite /index.html /index.php permanent; if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; break; } } } }
|
当服务器 ocalhost:8080
挂掉时,nginx
能将请求自动转向服务器 192.168.101.9:8080
。上面还加了一个 weight
属性,此属性表示各服务器被访问到的权重,weight
越高被访问到的几率越高。