目錄
1 nginx_upstream_check_module
可以利用第三方Nginx插件監控代理后端節點的服務器。
淘寶技術團隊開發了一個Tengine(Nginx的分支)模塊nginx_upstream_check_module,用於提供主動式后端服務器健康檢查。通過它可以檢測后端realserver的健康狀態,如果后端的realserver不可用,則所有的請求就不會轉發到該節點上。
Tengine原生支持這個模塊,而Nginx則需要通過打補丁的方式將該模塊添加到Nginx中。
補丁下載地址:
需要在上一篇文章基礎之上實現
環境准備
Hostname | IP | 說明 |
---|---|---|
lb01 | 192.168.90.5 | Nginx主負載均衡器 |
lb02 | 192.168.90.6 | Nginx輔負載均衡器 |
web01 | 192.168.90.8 | web01服務器 |
web02 | 192.168.90.7 | web02服務器 |
VIP:192.168.90.3
2 Nginx中加載並配置此模塊
-
# 在負載lb01中測試
-
[root @lb01 ~]# cd /home/oldboy/tools/
-
[root @lb01 tools]# wget -q https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
-
[root @lb01 tools]# ls
-
master nginx -1.6.3 nginx-1.6.3.tar.gz
-
[root @lb01 tools]# unzip master
-
[root @lb01 tools]# ls
-
master nginx -1.6.3 nginx-1.6.3.tar.gz nginx_upstream_check_module-master
-
-
# 由於是對源程序打補丁,所以還需要nginx軟件包
-
[root @lb01 tools]# cd nginx-1.6.3
-
[root @lb01 nginx-1.6.3]# patch -p1 ../nginx_upstream_check_module-master/check_1.5.12+.patch
-
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
-
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
-
patching file src/http/ngx_http_upstream_round_robin.c
-
patching file src/http/ngx_http_upstream_round_robin.h
-
-
# 編譯參數要和以前一致
-
[root @lb01 nginx-1.6.3]# ./configure --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --add-module=/home/oldboy/tools/nginx_upstream_check_module-master/ --prefix=/application/nginx-1.6.3/
-
-
# 如果是新裝的nginx則繼續執行下面make install一步,如果給已經安裝的nginx系統打監控補丁就不用執行make install了,這里直接make就行
-
# make 的作用就是重新生成Nginx二進制啟動命令而已。
-
make
-
-
# 操作前最好先備份
-
[root @lb01 nginx-1.6.3]# mv /application/nginx/sbin/nginx{,.ori}
-
-
# 將打過補丁的nginx二進制程序復制到/application/nginx/sbin/目錄下
-
[root @lb01 nginx-1.6.3]# cp ./obj/nginx /application/nginx/sbin
-
# 檢測nginx語法是否正常
-
/application/nginx/sbin/nginx -t
-
/application/nginx/sbin/nginx -v # 查看版本
-
-
# 修改配置文件如下,注意location中的default_pools要刪掉,否則會跳轉到10.0.0.7中的主頁面
-
[root @lb01 ~]# vim /application/nginx/conf/nginx.conf
-
worker_processes 1;
-
events {
-
worker_connections 1024;
-
}
-
http {
-
include mime.types;
-
default_type application/octet-stream;
-
sendfile on;
-
keepalive_timeout 65;
-
-
upstream static_pools{
-
server 192.168.90.7:80 weight=1;
-
check interval= 3000 rise=2 fall=5 timeout=1000 type=http;
-
}
-
upstream upload_pools{
-
server 192.168.90.7:8080 weight=1;
-
check interval= 3000 rise=2 fall=5 timeout=1000 type=http;
-
}
-
upstream default_pools{
-
server 192.168.90.8:80 weight=1;
-
check interval= 3000 rise=2 fall=5 timeout=1000 type=http;
-
}
-
-
server {
-
listen 80;
-
server_name www.rsq.com;
-
location /static/ {
-
proxy_pass http://static_pools;
-
include proxy.conf;
-
}
-
location /upload/ {
-
proxy_pass http://upload_pools;
-
include proxy.conf;
-
}
-
location /status {
-
check_status;
-
access_log off;
-
}
-
}
-
}
-
[root @lb01 nginx-1.6.3]# /application/nginx/sbin/nginx -t
-
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
-
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
上面配置的意思是,對三個負載均衡pools條目中的所有節點,每隔3秒檢測一次,請求2次正常則標記realserver狀態為up,如果檢測5次都失敗,則標記realserver的狀態為down,超市時間為1秒,檢查的協議是http。
詳細用法可參見官網
http://tengine.taobao.org/document_cn/http_upstream_check_cn.html
3 web頁面測試
把web02中的nginx服務停掉,過一會刷新看結果
基於nginx_upstream_check_module此模塊可以實現對Nginx Web節點(web01和web02)進行健康檢查
轉載至https://blog.csdn.net/mr_rsq/article/details/80399642