前言
本文主要是針對Nginx安裝、負載均衡配置,以及fair智能選舉、check后端節點檢查擴展功能如何擴展,進行講解說明。
fair模塊: upstream-fair,“公平的”Nginx 負載均衡模塊,增強了Nginx 提供的round-robin負載均衡算法,可以跟蹤后端服務器的負載來分發請求。
chek模塊:nginx_upstream_check_module,更專業的負載均衡器內節點的健康檢查。這個由淘寶技術團隊開發的 nginx 模塊 nginx_upstream_check_module,通過它可以用來檢測后端 realserver 的健康狀態。如果后端 realserver 不可用,則所有的請求就不會轉發到該節點上。
目錄
- 前期准備
- upstream-fair模塊
- upstream_check_module后台服務器健康檢測模塊
- Nginx安裝
- 負載均衡配置
- 心得
前期准備
程序
為了方便安裝,所有的源碼下載,都放在 /tmp/ 目錄下。
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.04
upstream-fair模塊
解壓 upstream-fair 源碼包
ztlsir@ztlsir-Virtual-Machine:/tmp$ unzip nginx-upstream-fair-master.zip
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
心得
在實現fair和check模塊時,走了很多坑。最初想着直接用淘寶的Tengine就不用安裝check模塊了,但是在安裝上fair模塊后,運行Tengine總是報錯。就算是修改了fair代碼,也還是不行,最后不得已又換回了Nginx。
如果
