Nginx+Tomcat實現負載平衡_nginx

來源:互聯網
上載者:User

一、負載平衡配置實現
1、反向 Proxy

2、實現代理(192.168.1.161代理到192.168.1.62)

worker_processes 1;events{    worker_connections 1024;}http{    include mine.types;    default_type application/octet-stream;    senfile on;    keepalive_timeout 65;    server{        listen 80;        server_name locahost;        location / {            #root html;            index index.html index.htm;            proxy_pass http://192.168.1.62:8080; # 代理到http://192.168.1.62:8080        }        error_page 500 502 503 504 /50x.html;        location = /50x.html {            root html;        }    }}重啟nginx:./nginx -s reload


二、負載平衡時Session的處理策略
1.實現基本的負載平衡

# 設定伺服器組,策略為預設策略,平均輪詢(1)upstream tomcats {    server 192.168.1.62:8080;    server 192.168.1.63:8080;}# 設定伺服器組,策略為ip_hash策略,使用者鎖定到一台伺服器上(2)upstream tomcats {    ip_hash;    server 192.168.1.62:8080;    server 192.168.1.63:8080;}# 配置用於負載平衡的伺服器群資訊(5)upstream backends{    server 192.168.1.62:8080 backup;    server 192.168.1.63;    # weight:權重,值越高負載越大(由權重決定的輪詢)    # server 192.168.1.4 weight=5;    # backup:備份機,只有非備份機都掛掉才啟用;    server 192.168.1.4 backup;    # down:停機標誌,不會被訪問,主要用於臨時的停機維護    server 192.168.1.65 down;     # max_fails:達到指定次數認為伺服器掛掉    # fail_timeout:掛掉之後過多久再去測試是否已恢複    server 192.168.1.66 max_fails=2 fail_timeout=60s;}worker_processes 1;events{    worker_connections 1024;}http{    include mine.types;    default_type application/octet-stream;    senfile on;    keepalive_timeout 65;    server{        listen 80;        server_name locahost;        location / {            #root html;            index index.html index.htm;            proxy_pass http://tomcats ; # 將當前的訪問發送到伺服器組        }        error_page 500 502 503 504 /50x.html;        location = /50x.html {            root html;        }    }}


每次重新整理,被代理到的伺服器都不同,預設平均輪詢。均衡策略包括
(1)none 輪詢,權重由weight決定
(2)ip_hash,某一個使用者綁定到某個伺服器
(3)fair策略,根據伺服器響應的快慢進行動態分配,響應快負載大
(4)url_hash,url綁定一個伺服器上

三、叢集環境中應用代碼應注意的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.