Nginx安裝負載均衡配置 fair check擴展

Nginx安裝負載均衡配置 fair check擴展

前言

本文主要是針對Nginx安裝、負載均衡配置,以及fair智能選舉、check後端節點檢查擴展功能如何擴展,進行講解說明。html

fair模塊: upstream-fair,「公平的」Nginx 負載均衡模塊,加強了Nginx 提供的round-robin負載均衡算法,能夠跟蹤後端服務器的負載來分發請求。mysql

chek模塊:nginx_upstream_check_module,更專業的負載均衡器內節點的健康檢查。這個由淘寶技術團隊開發的 nginx 模塊 nginx_upstream_check_module,經過它能夠用來檢測後端 realserver 的健康狀態。若是後端 realserver 不可用,則全部的請求就不會轉發到該節點上。nginx

 

目錄

  • 前期準備
  • upstream-fair模塊
  • upstream_check_module後臺服務器健康檢測模塊
  • Nginx安裝
  • 負載均衡配置
  • 心得

 

前期準備

程序

爲了方便安裝,全部的源碼下載,都放在 /tmp/ 目錄下。git

nginx-1.14.0 nginx程序源碼

下載地址:http://nginx.org/download/nginx-1.14.0.tar.gz

nginx-upstream-fair-master fair模塊源碼

官方github下載地址:https://github.com/gnosek/nginx-upstream-fair
說明:若是從github下載最新版本,在安裝到nginx 1.14.0版本時,會報出編譯錯誤。須要對源碼作一些修改,修改參照(若是你看到這篇文章時,github主已經修改了該bug,或者你用的是nginx 1.14.0如下版本,請忽視...):https://github.com/gnosek/nginx-upstream-fair/pull/27/commits/ff979a48a0ccb9217437021b5eb9378448c2bd9e
對於比較懶的童鞋,這裏提供了已經修改好的源碼包:https://files.cnblogs.com/files/ztlsir/nginx-upstream-fair-master.zip

nginx_upstream_check_module-master check模塊源碼

下載地址:https://github.com/yaoweibin/nginx_upstream_check_module

Nginx負載均衡反向代理選舉方式

  • 輪詢(默認)
  • ip_hash
  • weight
  • fair(第三方)
  • url_hash(第三方)。

 

系統

Ubuntu 14.04github

 

upstream-fair模塊

解壓 upstream-fair 源碼包算法

ztlsir@ztlsir-Virtual-Machine:/tmp$ unzip nginx-upstream-fair-master.zip  sql

 

upstream_check_module後臺服務器健康檢測模塊

解壓 upstream_check_module 源碼包後端

ztlsir@ztlsir-Virtual-Machine:/tmp$ unzip nginx_upstream_check_module-master.zip

給 upstream-fair 擴展 upstream_check_module 的 upstream_fair.patch 補丁。不然,當負載均衡採用fair模式時,fair將會不支持check服務器

ztlsir@ztlsir-Virtual-Machine:/tmp$ cd nginx-upstream-fair-master/ #進入纔將解壓的upstream-fair源碼目錄 
ztlsir@ztlsir-Virtual-Machine:/tmp/nginx-upstream-fair-master$ patch -p1 < ../nginx_upstream_check_module-master/upstream_fair.patch #打補丁,對patch有疑問的童鞋,自行百度

 

Nginx安裝

解壓 Nginx 1.14.0 源碼包網絡

ztlsir@ztlsir-Virtual-Machine:/tmp$ tar zxvf nginx-1.14.0.tar.gz

給Nginx擴展 upstream_check_module 的 check_1.12.1+.patch 補丁。

ztlsir@ztlsir-Virtual-Machine:/tmp$ cd nginx-1.14.0/ #進入纔將解壓的nginx源碼目錄 
ztlsir@ztlsir-Virtual-Machine:/tmp/nginx-1.14.0$ patch -p1 < ../nginx_upstream_check_module-master/check_1.12.1+.patch #打補丁

安裝Nginx

複製代碼

ztlsir@ztlsir-Virtual-Machine:/tmp/nginx-1.14.0$ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --add-module=/tmp/nginx_upstream_check_module-master --add-module=/tmp/nginx-upstream-fair-master  #配置,安裝目錄:/usr/local/nginx
ztlsir@ztlsir-Virtual-Machine:/tmp/nginx-1.14.0$ sudo make && sudo make install  #編譯、安裝,此處用sudo,由於在"/usr/local/"下建立nginx目錄須要root權限

複製代碼

在配置和安裝時,可能會出現pcre、ssl、complier、zlib等相關錯誤,能夠參考:Linux Nginx安裝以及可能出現錯誤

 

負載均衡配置

修改安裝目錄的擁有用戶

ztlsir@ztlsir-Virtual-Machine:/tmp$ cd /usr/local/  #進入到local目錄
ztlsir@ztlsir-Virtual-Machine:/usr/local$ sudo chown -R ztlsir nginx/  #修改nginx安裝目錄的全部者爲ztlsir

 

修改配置文件

/usr/local/nginx/nginx.conf:

複製代碼

#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 {
    check_shm_size 10M;
    include       mime.types;
    default_type  application/octet-stream;

    #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;

    proxy_next_upstream error timeout invalid_header;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    #引用vhosts文件夾下的配置文件
    include vhosts/*.conf;
 
}

複製代碼

/usr/local/nginx/vhosts的目錄文件

ztlsir@ztlsir-Virtual-Machine:/usr/local/nginx/conf/vhosts$ ls
1001.conf  1002.conf  1003.conf

vhosts是自定義的文件夾。在該文件夾下,是根據端口進行負載配置的,此處選取其中一個文件進行展現,其餘文件都大同小異。

/usr/local/nginx/vhosts/1001.conf:

複製代碼

upstream localhost1001 {
            fair;
            server  192.168.120.10:1001;
            server  192.168.120.11:1001;
            check interval=3000 rise=2 fall=5 timeout=1000 type=http;
            #在Web服務配置了默認文檔時,可使用以下配置
            #但當服務沒有配置時,須要修改成:check_http_send "GET /temp.html HTTP/1.0\r\n\r\n";
            #其中「/temp.html」表示根目錄下的一個temp.html文件
            check_http_send "HEAD / HTTP/1.0\r\n\r\n";
            check_http_expect_alive http_2xx http_3xx;
    }
    server {
        listen       1001;
        server_name  localhost;

        #charset koi8-r;
    
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass  http://localhost1001;
            proxy_redirect  off;
            proxy_set_header Host $host:1001;
            proxy_connect_timeout       100s;
            proxy_read_timeout          150s;
            proxy_send_timeout          100s;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

複製代碼

 在如上配置中,負載了兩個後端節點: 192.168.120.10:1001 和192.168.120.11:1001 ,負載選舉模式採用 fair 

 check 配置了健康檢查的基本規則。 interval=3000 每隔3秒(3000毫秒)檢測一次, rise=2 請求2次正常則標記 realserver狀態爲up, fall=5 檢測 5 次都失敗則標記 realserver的狀態爲down, timeout=1000 超時時間爲1秒,超時即失敗。如下爲詳細說明:

複製代碼

Syntax: check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false] [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port]
Default: 若是沒有配置參數,默認值是:interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp
Context: upstream

Check指令參數意義:
    - interval:向後端發送的健康檢查包的間隔。
    - fall(fall_count): 若是連續失敗次數達到fall_count,服務器就被認爲是down。
    - rise(rise_count): 若是連續成功次數達到rise_count,服務器就被認爲是up。
    - timeout: 後端健康請求的超時時間。
    - default_down: 設定初始時服務器的狀態,若是是true,就說明默認是down的,若是是false,就是up的。默認值是true,也就是一開始服務器認爲是不可用,要等健康檢查包達到必定成功次數之後纔會被認爲是健康的。
    - type:健康檢查包的類型,如今支持如下多種類型
        - tcp:簡單的tcp鏈接,若是鏈接成功,就說明後端正常。
        - ssl_hello:發送一個初始的SSL hello包並接受服務器的SSL hello包。
        - http:發送HTTP請求,經過後端的回覆包的狀態來判斷後端是否存活。
        - mysql: 向mysql服務器鏈接,經過接收服務器的greeting包來判斷後端是否存活。
        - ajp:向後端發送AJP協議的Cping包,經過接收Cpong包來判斷後端是否存活。
    - port: 指定後端服務器的檢查端口。你能夠指定不一樣於真實服務的後端服務器的端口,好比後端提供的是443端口的應用,你能夠去檢查80端口的狀態來判斷後端健康情況。默認是0,表示跟後端server提供真實服務的端口同樣。該選項出現於Tengine-1.4.0。

複製代碼

 check_http_send 配置了健康檢查包發送的內容。 "HEAD / HTTP/1.0\r\n\r\n" 表示採用 HEAD 方法,訪問路徑爲 / 程序根目錄(默認文檔)。如下爲詳細說明:

複製代碼

Syntax: check_http_send http_packet
Default: "GET / HTTP/1.0\r\n\r\n"
Context: upstream

    -http_packet:當採用長鏈接進行健康檢查時,需在該指令中添加keep-alive請求頭,如:"HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"。 同時,在採用"GET"方法的狀況下,請求uri的size不宜過大,確保能夠在1個interval內傳輸完成,不然會被健康檢查模塊視爲後端服務器或網絡異常

複製代碼

 check_http_expect_alive 指定HTTP回覆的成功狀態,默認認爲2XX和3XX的狀態是健康的。如下爲詳細說明:

Syntax: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
Default: http_2xx | http_3xx
Context: upstream

關於 check 模塊的詳細說明,能夠參照:ngx_http_upstream_check_module

相關文章
相關標籤/搜索