Nginx負(fù)載均衡與健康檢查使用詳解
一、nginx原生模塊介紹
我們在使用nginx做反向代理都會使用到以下兩個模塊:
1、ngx_http_proxy_module
定義允許將請求傳遞到另一臺服務(wù)器。此模塊下常用指令如下:
proxy_pass proxy_cache proxy_connect_timeout proxy_read_timeout proxy_send_timeout proxy_next_upstream
2、ngx_http_upstream_module
用于定義可由proxy_pass,fastcgi_pass等指令引用的服務(wù)器組。
此模塊下常用指令如下:
upstream server ip_hash
默認(rèn)負(fù)載均衡配置
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}此時nginx默認(rèn)的負(fù)載均衡策略是輪詢外,還有其他默認(rèn)參數(shù),如下:
http {
upstream myapp1 {
server srv1.example.com weight=1 max_fails=1 fail_timeout=10;
server srv2.example.com weight=1 max_fails=1 fail_timeout=10;
server srv3.example.com weight=1 max_fails=1 fail_timeout=10;
}
?
server {
listen 80;
proxy_send_timeout=60;
proxy_connect_timeout=60;
proxy_read_timeout=60;
proxy_next_upstream=error timeout;
?
location / {
proxy_pass http://myapp1;
}
}
}二、nginx_upstream_check_module模塊
借助淘寶技術(shù)團(tuán)隊開發(fā)的nginx??靚ginx_upstream_check_module來檢測后方realserver的健康狀態(tài),如果后端服務(wù)器不可用,則會將其踢出upstream,所有的請求不轉(zhuǎn)發(fā)到這臺服務(wù)器。當(dāng)期恢復(fù)正常時,將其加入upstream。
在淘寶自己的tengine上是自帶了該模塊的,大家可以訪問淘寶Tengine官網(wǎng)來獲取該版本的nginx,也可以到Gitbub
如果沒有使用淘寶的tengine的話,可以通過補(bǔ)丁的方式來添加該模塊到我們自己的nginx中。
下載地址1:https://github.com/yaoweibin/nginx_upstream_check_module
#打補(bǔ)丁 #注意不同版本對應(yīng)的補(bǔ)丁 cd nginx-1.6.0 patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx1.6 --sbin-path=/usr/local/nginx1.6 --conf-path=/usr/local/nginx1.6/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_sub_module --with-pcre=/usr/local/src/nginx/pcre-8.36 --with-zlib=/usr/local/src/nginx/zlib-1.2.8 --add-module=/usr/local/src/nginx/ngx_cache_purge-2.1 --add-module=/usr/local/src/nginx/headers-more-nginx-module-master --add-module=/usr/local/src/nginx/nginx_upstream_check_module-master make #不要執(zhí)行make install命令 cd /usr/local/nginx1.6 #備份命令 cp nginx nginx.bak nginx -s stop cp -r /usr/local/src/nginx/nginx-1.6.0/objs/nginx .
打完補(bǔ)丁后,可進(jìn)行如下配置:
http {
upstream cluster {
# simple round-robin
server 192.168.0.1:80;
server 192.168.0.2:80;
check interval=5000 rise=1 fall=3 timeout=4000;
#check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello;
#check interval=3000 rise=2 fall=5 timeout=1000 type=http;
#check_http_send "HEAD / HTTP/1.0\r\n\r\n";
#check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
location / {
proxy_pass http://cluster;
}
location /status {
check_status;
access_log off;
allow SOME.IP.ADD.RESS;
deny all;
}
}
}其中:
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: 如果沒有配置參數(shù),默認(rèn)值是:interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp
Context: upstream
該指令可以打開后端服務(wù)器的健康檢查功能。指令后面的參數(shù)意義是:
interval:向后端發(fā)送的健康檢查包的間隔,單位為毫秒。
fall(fall_count): 如果連續(xù)失敗次數(shù)達(dá)到fall_count,服務(wù)器就被認(rèn)為是down。
rise(rise_count): 如果連續(xù)成功次數(shù)達(dá)到rise_count,服務(wù)器就被認(rèn)為是up。
timeout: 后端健康請求的超時時間,單位毫秒。
default_down: 設(shè)定初始時服務(wù)器的狀態(tài),如果是true,就說明默認(rèn)是down的,如果是false,就是up的。默認(rèn)值是true,也就是一開始服務(wù)器認(rèn)為是不可用,要等健康檢查包達(dá)到一定成功次數(shù)以后才會被認(rèn)為是健康的。
type:健康檢查包的類型,現(xiàn)在支持以下多種類型:
tcp:簡單的tcp連接,如果連接成功,就說明后端正常。
ssl_hello:發(fā)送一個初始的SSL hello包并接受服務(wù)器的SSL hello包。
http:發(fā)送HTTP請求,通過后端的回復(fù)包的狀態(tài)來判斷后端是否存活。
mysql: 向mysql服務(wù)器連接,通過接收服務(wù)器的greeting包來判斷后端是否存活。
ajp:向后端發(fā)送AJP協(xié)議的Cping包,通過接收Cpong包來判斷后端是否存活。
port: 指定后端服務(wù)器的檢查端口。你可以指定不同于真實服務(wù)的后端服務(wù)器的端口,比如后端提供的是443端口的應(yīng)用,你可以去檢查80端口的狀態(tài)來判斷后端健康狀況。默認(rèn)是0,表示跟后端server提供真實服務(wù)的端口一樣。該選項出現(xiàn)于Tengine-1.4.0。
Syntax: check_keepalive_requests request_num
Default: 1
Context: upstream
該指令可以配置一個連接發(fā)送的請求數(shù),其默認(rèn)值為1,表示Tengine完成1次請求后即關(guān)閉連接。
Syntax: check_http_send http_packet
Default: "GET / HTTP/1.0\r\n\r\n"
Context: upstream
該指令可以配置http健康檢查包發(fā)送的請求內(nèi)容。為了減少傳輸數(shù)據(jù)量,推薦采用"HEAD"方法。
當(dāng)采用長連接進(jìn)行健康檢查時,需在該指令中添加keep-alive請求頭,如:"HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"。 同時,在采用"GET"方法的情況下,請求uri的size不宜過大,確保可以在1個interval內(nèi)傳輸完成,否則會被健康檢查模塊視為后端服務(wù)器或網(wǎng)絡(luò)異常。
Syntax: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
Default: http_2xx | http_3xx
Context: upstream
該指令指定HTTP回復(fù)的成功狀態(tài),默認(rèn)認(rèn)為2XX和3XX的狀態(tài)是健康的。例子如下:
server{
listen 80;
upstream test{
server 192.168.3.12:8080 weight=5 max_fails=3 fail_timeout=10s;
server 192.168.3.13:8080 weight=5 max_fails=3 fail_timeout=10s;
check interval=5000 rise=1 fall=3 timeout=4000 type=http default_down=false;
check_http_send "HEAD /index.html HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://test;
proxy_next_upstream error timeout http_500 http_502 http_503;
}
#后端階段健康狀態(tài)監(jiān)控
location /status {
check_status;
access_log off;
}
}以上我們同時使用了nginx原生的及淘寶的健康檢查模塊,但是淘寶的間隔時是毫秒級,而且可以自定義監(jiān)控url,定制監(jiān)控頁,響應(yīng)速度快,比原生的敏感度要高。
三、使用阿里巴巴的tengine實現(xiàn)后端節(jié)點狀態(tài)檢查
1、安裝tengine
unzip tengine-3.1.0.zip cd tengine-3.1.0/ ./configure --prefix=/usr/local/tengine --add-module=/root/tengine-3.1.0/modules/ngx_http_upstream_check_module && make && make install
2、修改配置文件
user nginx;
worker_processes 1;
error_log logs/error.log info;
error_log "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2G";
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
access_log "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G" main;
sendfile on;
keepalive_timeout 65;
gzip on;
upstream webs {
server 192.168.166.10:80;
server 192.168.166.13:80;
check interval=5000 rise=1 fall=3 timeout=4000 type=http default_down=false;
check_http_send "HEAD /index.html HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://webs;
}
location /status {
check_status html;
access_log off;
allow all;
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
nginx:413 Request Entity Too Large的處理辦法--修改 PHP上傳文件大小
在用 phpMyAdmin 進(jìn)行 sql 數(shù)據(jù)庫導(dǎo)入的時候,經(jīng)常需要上傳比較大的 sql 數(shù)據(jù)文件,而這時會常碰見 nginx報錯:413 Request Entity Too Large。解決此問題,根據(jù)上傳數(shù)據(jù)文件的大小進(jìn)行修改處理2014-06-06
Nginx HTTP:413 Request Entity Too Large解決方法
這篇文章主要介紹了Nginx HTTP:413 Request Entity Too Large解決方法,這個問題需要修改PHP配置以及Nginx配置才可以解決,需要的朋友可以參考下2015-07-07
Nginx之正向代理與反向代理進(jìn)階方式(支持https)
文章介紹了如何在Nginx中實現(xiàn)正向代理和反向代理對HTTP和HTTPS協(xié)議的支持,通過使用第三方模塊`ngx_http_proxy_connect_module`和Nginx內(nèi)置的`ngx_http_ssl_module`,可以實現(xiàn)Nginx對HTTPS的正向和反向代理2025-03-03

