nginx 反向代理及負載均衡
假設我們在 192.168.137.11:8080 上有一個web服務,在 192.168.137.12 配置了一臺 nginx,我們可以進行如下配置:
location / {
proxy_pass http://192.168.137.11:8080;
}
這樣配置後,我們對 192.168.137.12 的訪問都會被轉發到 192.168.137.11:8080,此時這臺 nginx 就是反向代理服務器。
負載均衡:
現在還有一臺 192.168.137.13:8080 提供與 192.168.137.11:8080 一樣的web 服務,nginx 通過下面的配置,可以實現負載均衡。
upstream loadbalance
server 192.168.137.11:8080;
server 192.168.137.48:8080;
#下面的3行配置是用於檢查上面配置的服務器的狀態的
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
server_name www.nginx1.com;
location / {
proxy_pass http://loadbalance;
root html;
index index.html index.htm;
}
location /status { #通過此URL(www.nginx1.com/status)可查看服務器的狀態。這功能被 tengine 增強了
check_status;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
nginx 反向代理及負載均衡