本次使用第三方模塊nginx_upstream_check_module的,要使用這個(gè)第三方模塊首先您需要進(jìn)行下載,然后通過patch命令將補(bǔ)丁打入您原有的Nginx源碼中,并且重新進(jìn)行編譯安裝。
下載nginx_upstream_check_module模塊
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
解壓
unzip master
解壓后的Nginx和nginx_upstream_check_module-master在同一目錄下
將補(bǔ)丁打入源碼(沒有patch命令使用yum -y install patch安裝)
cd nginx-1.6.3
patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch
出現(xiàn)以下代表成功(根據(jù)nginx版本選擇不同的check)
編譯安裝nginx
./configure –prefix=/usr/local/nginx –add-module=../nginx_upstream_check_module-master –with-http_stub_status_module
make && make install
通過以上的步驟,第三方的nginx_upstream_check_module模塊就在Nginx中準(zhǔn)備好了。接下來我們講解一下如何使用這個(gè)模塊。首先看一下upstream的配置信息
interval:必要參數(shù),檢查請求的間隔時(shí)間。
fall:當(dāng)檢查失敗次數(shù)超過了fall,這個(gè)服務(wù)節(jié)點(diǎn)就變成down狀態(tài)。
rise:當(dāng)檢查成功的次數(shù)超過了rise,這個(gè)服務(wù)節(jié)點(diǎn)又會變成up狀態(tài)。
timeout:請求超時(shí)時(shí)間,超過等待時(shí)間后,這次檢查就算失敗。
default_down:后端服務(wù)器的初始狀態(tài)。默認(rèn)情況下,檢查功能在Nginx啟動的時(shí)候?qū)阉泻蠖斯?jié)點(diǎn)的狀態(tài)置為down,檢查成功后,在置為up。
type:這是檢查通信的協(xié)議類型,默認(rèn)為http。以上類型是檢查功能所支持的所有協(xié)議類型。
一個(gè)完整的nginx配置信息如下nginx.conf
worker_processes 4;
error_log logs/error.log;
events {
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream ekp {
server 10.1.1.131:8080;
server 10.1.1.132:8080;
server 10.1.1.135:8080;
check interval=3000 rise=2 fall=5;
check_keepalive_requests 100;
check_http_send “HEAD / HTTP/1.1rnConnection: keep-alivernrn”;
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
server_name www.test.com;
access_log /usr/local/nginx/logs/access.log;
location / {
root ekp;
index index.html index.htm index.jsp;
proxy_pass http://ekp;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 200m;
proxy_connect_timeout 10;
proxy_read_timeout 300;
proxy_send_timeout 300;
}
location /nstatus {
check_status;
access_log on;
}
}
}
PS:這里配置按照參考文檔加timeout=1000 type=http 否則無法負(fù)載均衡
訪問測試 http://ip/nstatus