Nginx負(fù)載均衡與健康檢查使用詳解
一、nginx原生模塊介紹
我們?cè)谑褂胣ginx做反向代理都會(huì)使用到以下兩個(gè)模塊:
1、ngx_http_proxy_module
定義允許將請(qǐng)求傳遞到另一臺(tái)服務(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;
}
}
}此時(shí)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)隊(duì)開發(fā)的nginx??靚ginx_upstream_check_module來檢測(cè)后方realserver的健康狀態(tài),如果后端服務(wù)器不可用,則會(huì)將其踢出upstream,所有的請(qǐng)求不轉(zhuǎn)發(fā)到這臺(tái)服務(wù)器。當(dāng)期恢復(fù)正常時(shí),將其加入upstream。
在淘寶自己的tengine上是自帶了該模塊的,大家可以訪問淘寶Tengine官網(wǎng)來獲取該版本的nginx,也可以到Gitbub
如果沒有使用淘寶的tengine的話,可以通過補(bǔ)丁的方式來添加該模塊到我們自己的nginx中。
下載地址1:https://github.com/yaoweibin/nginx_upstream_check_module
#打補(bǔ)丁 #注意不同版本對(duì)應(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: 后端健康請(qǐng)求的超時(shí)時(shí)間,單位毫秒。
default_down: 設(shè)定初始時(shí)服務(wù)器的狀態(tài),如果是true,就說明默認(rèn)是down的,如果是false,就是up的。默認(rèn)值是true,也就是一開始服務(wù)器認(rèn)為是不可用,要等健康檢查包達(dá)到一定成功次數(shù)以后才會(huì)被認(rèn)為是健康的。
type:健康檢查包的類型,現(xiàn)在支持以下多種類型:
tcp:簡(jiǎn)單的tcp連接,如果連接成功,就說明后端正常。
ssl_hello:發(fā)送一個(gè)初始的SSL hello包并接受服務(wù)器的SSL hello包。
http:發(fā)送HTTP請(qǐng)求,通過后端的回復(fù)包的狀態(tài)來判斷后端是否存活。
mysql: 向mysql服務(wù)器連接,通過接收服務(wù)器的greeting包來判斷后端是否存活。
ajp:向后端發(fā)送AJP協(xié)議的Cping包,通過接收Cpong包來判斷后端是否存活。
port: 指定后端服務(wù)器的檢查端口。你可以指定不同于真實(shí)服務(wù)的后端服務(wù)器的端口,比如后端提供的是443端口的應(yīng)用,你可以去檢查80端口的狀態(tài)來判斷后端健康狀況。默認(rèn)是0,表示跟后端server提供真實(shí)服務(wù)的端口一樣。該選項(xiàng)出現(xiàn)于Tengine-1.4.0。
Syntax: check_keepalive_requests request_num
Default: 1
Context: upstream
該指令可以配置一個(gè)連接發(fā)送的請(qǐng)求數(shù),其默認(rèn)值為1,表示Tengine完成1次請(qǐng)求后即關(guān)閉連接。
Syntax: check_http_send http_packet
Default: "GET / HTTP/1.0\r\n\r\n"
Context: upstream
該指令可以配置http健康檢查包發(fā)送的請(qǐng)求內(nèi)容。為了減少傳輸數(shù)據(jù)量,推薦采用"HEAD"方法。
當(dāng)采用長(zhǎng)連接進(jìn)行健康檢查時(shí),需在該指令中添加keep-alive請(qǐng)求頭,如:"HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"。 同時(shí),在采用"GET"方法的情況下,請(qǐng)求uri的size不宜過大,確保可以在1個(gè)interval內(nèi)傳輸完成,否則會(huì)被健康檢查模塊視為后端服務(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;
}
}以上我們同時(shí)使用了nginx原生的及淘寶的健康檢查模塊,但是淘寶的間隔時(shí)是毫秒級(jí),而且可以自定義監(jiān)控url,定制監(jiān)控頁(yè),響應(yīng)速度快,比原生的敏感度要高。
三、使用阿里巴巴的tengine實(shí)現(xiàn)后端節(jié)點(diǎn)狀態(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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
linux下Nginx+Tomcat負(fù)載均衡配置方法
這篇文章主要介紹了linux下Nginx+Tomcat負(fù)載均衡配置方法,需要的朋友可以參考下2016-09-09
詳解Nginx 動(dòng)態(tài) DNS 反向代理的幾種寫法
這篇文章主要介紹了詳解Nginx 動(dòng)態(tài) DNS 反向代理的幾種寫法,詳細(xì)的介紹了Nginx 動(dòng)態(tài) DNS 反向代理的4種方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-05-05
Nginx?HttpHeader增加幾個(gè)關(guān)鍵的安全選項(xiàng)問題小結(jié)
本文給大家介紹Nginx?HttpHeader增加幾個(gè)關(guān)鍵的安全選項(xiàng)問題小結(jié),結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-12-12
Nginx實(shí)現(xiàn)瀏覽器可實(shí)時(shí)查看訪問日志的步驟詳解
我們經(jīng)常需要在頁(yè)面上實(shí)時(shí)查看nginx的日志輸出,并且能在頁(yè)面上顯示,那么下面小編就給大家說下怎么在瀏覽器上實(shí)時(shí)動(dòng)態(tài)的查看nginx的訪問日志,有需要的朋友們可以參考借鑒。2016-09-09
Nginx實(shí)現(xiàn)集群的負(fù)載均衡配置過程解析
這篇文章主要為大家詳細(xì)介紹了Nginx實(shí)現(xiàn)集群的負(fù)載均衡配置過程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
詳解使用Nginx和uWSGI配置Python的web項(xiàng)目的方法
這篇文章主要介紹了使用Nginx和uWSGI配置Python的web項(xiàng)目的方法,與其他CGI連接方式相比uwsgi的連接性能也較為出眾,需要的朋友可以參考下2015-12-12

