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ù)載均衡策略是輪詢(xún)外,還有其他默認(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ì)開(kāi)發(fā)的nginx??靚ginx_upstream_check_module來(lái)檢測(cè)后方realserver的健康狀態(tài),如果后端服務(wù)器不可用,則會(huì)將其踢出upstream,所有的請(qǐng)求不轉(zhuǎn)發(fā)到這臺(tái)服務(wù)器。當(dāng)期恢復(fù)正常時(shí),將其加入upstream。
在淘寶自己的tengine上是自帶了該模塊的,大家可以訪問(wèn)淘寶Tengine官網(wǎng)來(lái)獲取該版本的nginx,也可以到Gitbub
如果沒(méi)有使用淘寶的tengine的話,可以通過(guò)補(bǔ)丁的方式來(lái)添加該模塊到我們自己的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: 如果沒(méi)有配置參數(shù),默認(rèn)值是:interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp
Context: upstream
該指令可以打開(kāi)后端服務(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,就說(shuō)明默認(rèn)是down的,如果是false,就是up的。默認(rèn)值是true,也就是一開(kāi)始服務(wù)器認(rèn)為是不可用,要等健康檢查包達(dá)到一定成功次數(shù)以后才會(huì)被認(rèn)為是健康的。
type:健康檢查包的類(lèi)型,現(xiàn)在支持以下多種類(lèi)型:
tcp:簡(jiǎn)單的tcp連接,如果連接成功,就說(shuō)明后端正常。
ssl_hello:發(fā)送一個(gè)初始的SSL hello包并接受服務(wù)器的SSL hello包。
http:發(fā)送HTTP請(qǐng)求,通過(guò)后端的回復(fù)包的狀態(tài)來(lái)判斷后端是否存活。
mysql: 向mysql服務(wù)器連接,通過(guò)接收服務(wù)器的greeting包來(lái)判斷后端是否存活。
ajp:向后端發(fā)送AJP協(xié)議的Cping包,通過(guò)接收Cpong包來(lái)判斷后端是否存活。
port: 指定后端服務(wù)器的檢查端口。你可以指定不同于真實(shí)服務(wù)的后端服務(wù)器的端口,比如后端提供的是443端口的應(yīng)用,你可以去檢查80端口的狀態(tài)來(lá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不宜過(guò)大,確保可以在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è)參考,也希望大家多多支持腳本之家。
- Nginx HTTP反向代理負(fù)載均衡實(shí)驗(yàn)教程
- Nginx搭建負(fù)載均衡及常見(jiàn)問(wèn)題處理
- Nginx幾種負(fù)載均衡模式的實(shí)現(xiàn)示例
- Nginx負(fù)載均衡通用方案詳解
- 詳解Nginx中常見(jiàn)負(fù)載均衡策略配置與使用場(chǎng)景
- Nginx 負(fù)載均衡和緩存配置最佳實(shí)踐
- Nginx+Tomcat負(fù)載均衡群集全過(guò)程
- Nginx部署負(fù)載均衡服務(wù)的步驟全解析
- nginx負(fù)載均衡配置方式
- Nginx 4層轉(zhuǎn)發(fā)TCP流量實(shí)現(xiàn)負(fù)載代理
相關(guān)文章
nginx:413 Request Entity Too Large的處理辦法--修改 PHP上傳文件大小
在用 phpMyAdmin 進(jìn)行 sql 數(shù)據(jù)庫(kù)導(dǎo)入的時(shí)候,經(jīng)常需要上傳比較大的 sql 數(shù)據(jù)文件,而這時(shí)會(huì)常碰見(jiàn) nginx報(bào)錯(cuò):413 Request Entity Too Large。解決此問(wèn)題,根據(jù)上傳數(shù)據(jù)文件的大小進(jìn)行修改處理2014-06-06
Nginx HTTP:413 Request Entity Too Large解決方法
這篇文章主要介紹了Nginx HTTP:413 Request Entity Too Large解決方法,這個(gè)問(wèn)題需要修改PHP配置以及Nginx配置才可以解決,需要的朋友可以參考下2015-07-07
Nginx之正向代理與反向代理進(jìn)階方式(支持https)
文章介紹了如何在Nginx中實(shí)現(xiàn)正向代理和反向代理對(duì)HTTP和HTTPS協(xié)議的支持,通過(guò)使用第三方模塊`ngx_http_proxy_connect_module`和Nginx內(nèi)置的`ngx_http_ssl_module`,可以實(shí)現(xiàn)Nginx對(duì)HTTPS的正向和反向代理2025-03-03
Nginx負(fù)載均衡/SSL配置的實(shí)現(xiàn)
這篇文章主要介紹了Nginx負(fù)載均衡/SSL配置的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Nginx代理Redis哨兵主從配置的實(shí)現(xiàn)
本文主要介紹了Nginx代理Redis哨兵主從配置的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

