nginx 負載均衡時,一臺tomcat當機時的問題 自動切換

hunhun1122發表於2017-08-19

如果Nginx沒有僅僅只能代理一臺伺服器的話,那它也不可能像今天這麼火,Nginx可以配置代理多臺伺服器,當一臺伺服器當機之後,仍能保持系統可用。具體配置過程如下:

1. 在http節點下,新增upstream節點。

upstream linuxidc {
      server 10.0.6.108:7080;
      server 10.0.0.85:8980;
}<wbr><wbr><wbr><wbr><wbr><wbr>

  2.  將server節點下的location節點中的proxy_pass配置為:http:// + upstream名稱,即“
http://linuxidc”.<wbr><wbr>


location / {
            root  html;
            index  index.html index.htm;
            proxy_pass http://linuxidc;
}<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>

    3.  現在負載均衡初步完成了。upstream按照輪詢(預設)方式進行負載,每個請求按時間順序逐一分配到不同的後端伺服器,如果後端伺服器down掉,能 自動剔除。雖然這種方式簡便、成本低廉。但缺點是:可靠性低和負載分配不均衡。適用於圖片伺服器叢集和純靜態頁面伺服器叢集。<wbr><wbr><wbr>

    除此之外,upstream還有其它的分配策略,分別如下:<wbr><wbr>

    weight(權重)<wbr><wbr>

    指定輪詢機率,weight和訪問比率成正比,用於後端伺服器效能不均的情況。如下所示,10.0.0.88的訪問比率要比10.0.0.77的訪問比率高一倍。<wbr><wbr>

upstream linuxidc{
      server 10.0.0.77 weight=5;
      server 10.0.0.88 weight=10;
}<wbr><wbr><wbr><wbr><wbr><wbr>

    ip_hash(訪問ip)<wbr><wbr>

    每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題。<wbr><wbr>

upstream favresin{
      ip_hash;
      server 10.0.0.10:8080;
      server 10.0.0.11:8080;
}<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>

    fair(第三方)<wbr><wbr>

    按後端伺服器的響應時間來分配請求,響應時間短的優先分配。與weight分配策略類似。<wbr><wbr>

 upstream favresin{     
      server 10.0.0.10:8080;
      server 10.0.0.11:8080;
      fair;
}<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>

url_hash(第三方)

按訪問url的hash結果來分配請求,使每個url定向到同一個後端伺服器,後端伺服器為快取時比較有效。

注意:在upstream中加入hash語句,server語句中不能寫入weight等其他的引數,hash_method是使用的hash演算法。

 upstream resinserver{
      server 10.0.0.10:7777;
      server 10.0.0.11:8888;
      hash $request_uri;
      hash_method crc32;
}<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>

upstream還可以為每個裝置設定狀態值,這些狀態值的含義分別如下:

down 表示單前的server暫時不參與負載.

weight 預設為1.weight越大,負載的權重就越大。

max_fails :允許請求失敗的次數預設為1.當超過最大次數時,返回proxy_next_upstream 模組定義的錯誤.

fail_timeout : max_fails次失敗後,暫停的時間。

backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。

upstream bakend{ #定義負載均衡裝置的Ip及裝置狀態
      ip_hash;
      server 10.0.0.11:9090 down;
      server 10.0.0.11:8080 weight=2;
      server 10.0.0.11:6060;
      server 10.0.0.11:7070 backup;
}<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>

Nginx的配置與部署研究,Upstream負載均衡模組  http://www.linuxidc.com/Linux/2013-04/82526p2.htm<wbr>

CentOS 6.2實戰部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用Nginx搭建WEB伺服器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基於Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web伺服器全過程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3下Nginx效能調優 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3下配置Nginx載入ngx_pagespeed模組 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4安裝配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx安裝配置使用詳細筆記 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx日誌過濾 使用ngx_log_if不記錄特定日誌 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的詳細介紹請點這裡
Nginx 的下載地址請點這裡



用了nginx負載均衡後,在兩臺tomcat正常執行的情況下,訪問http://localhost 速度非常迅速,通過測試程式也可以看出是得到的負載均衡的效果,但是我們試驗性的把其中一臺tomcat(server localhost:8080)關閉後,再檢視http://localhost,發現反應呈現了一半反映時間快,一半反映時間非常非常慢的情況,但是最後都能得到正確結果.

 <wbr>

解決辦法:

問題解決,主要是proxy_connect_timeout   這個引數, 這個引數是連線的超時時間。 我設定成1,表示是1秒後超時會連線到另外一臺伺服器。
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

     upstream localhost {
       #ip_hash;
       server 127.0.0.1:8081;
       server 127.0.0.1:8080;
     }

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

    listen 80;
    server_name localhost;
    location /{
    proxy_pass http://localhost;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout       1;
    proxy_read_timeout          1;
    proxy_send_timeout          1;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>

    }
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>

相關文章