參考文檔:https://www.cnblogs.com/kevingrace/p/6685698.html
本次使用第三方模塊nginx_upstream_check_module的,要使用這個第三方模塊首先您需要進行下載,然后通過patch命令將補丁打入您原有的Nginx源碼中,並且重新進行編譯安裝。
下載nginx_upstream_check_module模塊
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
解壓
unzip master
解壓后的Nginx和nginx_upstream_check_module-master在同一目錄下
將補丁打入源碼(沒有patch命令使用yum -y install patch安裝)
cd nginx-1.6.3 patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch
出現以下代表成功(根據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中准備好了。接下來我們講解一下如何使用這個模塊。首先看一下upstream的配置信息
interval:必要參數,檢查請求的間隔時間。
fall:當檢查失敗次數超過了fall,這個服務節點就變成down狀態。
rise:當檢查成功的次數超過了rise,這個服務節點又會變成up狀態。
timeout:請求超時時間,超過等待時間后,這次檢查就算失敗。
default_down:后端服務器的初始狀態。默認情況下,檢查功能在Nginx啟動的時候將會把所有后端節點的狀態置為down,檢查成功后,在置為up。
type:這是檢查通信的協議類型,默認為http。以上類型是檢查功能所支持的所有協議類型。
一個完整的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.1\r\nConnection: keep-alive\r\n\r\n"; 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 否則無法負載均衡
訪問測試 http://ip/nstatus