i. 需求
nginx作為負載均衡服務器,用戶請求先到達nginx,再由nginx根據負載配置將請求轉發至 tomcat服務器。
nginx負載均衡服務器:192.168.101.3
tomcat1服務器:192.168.101.5
tomcat2服務器:192.168.101.6
ii. 配置
根據上邊的需求在nginx.conf文件中配置負載均衡,如下:
upstream tomcat_server_pool{
server 192.168.101.5:8080 weight=10;
server 192.168.101.6:8080 weight=10;
}
server {
listen 80;
server_name aaa.test.com;
location / {
proxy_pass http://tomcat_server_pool;
index index.jsp index.html index.htm;
}
}
節點說明: 在http節點里添加:
#定義負載均衡設備的 Ip及設備狀態 upstream myServer {
server 127.0.0.1:9090 down; server 127.0.0.1:8080 weight=2; server 127.0.0.1:6060; server 127.0.0.1:7070 backup; }
在需要使用負載的Server節點下添加
proxy_pass http://myServer;
upstream 每個設備的狀態:
down 表示單前的server暫時不參與負載 weight 默認為1.weight越大,負載的權重就越大。 max_fails :允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤 fail_timeout:max_fails 次失敗后,暫停的時間。 backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這台機器壓力會最輕。 |
iii. 測試
請求aaa.test.com,通過nginx負載均衡,將請求轉發到tomcat服務器。
通過觀察tomcat的訪問日志或tomcat訪問頁面即可知道當前請求由哪個tomcat服務器受理。