nginx 負載均衡web查看狀態nginx_upstream_check_module
最近在做nginx負載均衡的時候發現nginx本身沒有界面的管理工具,也沒有發現其他的文本的管理工具。希望知道的朋友可以留言告訴我. 發現taobao在開源了自己的nginx 定製版本里面,有一個模塊(nginx_upstream_check_module),這個模塊就是用來查看nginx 負載均衡時候的realserver的狀態的. 經過實驗已經成功加入nginx 再此做個記錄和分享。
lnmp 環境的安裝請參照我的生產環境的lnmp 的安裝,在安裝nginx的時候參照下面的安裝:
http://renzhenxing.blog.51cto.com/blog/728846/1321572
下載nginx_upstream_check_module:
cd /usr/local/src/ wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip unzip master.zip
安裝nginx的支持:pcre
tar-zxvf pcre-8.21.tar.gz cd pcre-8.21 ./configure && make && makeinstall
安裝nginx-1.3.0
tar -zxvf nginx-1.3.0.tar.gz cd nginx-1.3.0
給nginx打上nginx_upstream_check_module-master
的包
(注:因nginx版本更新,1.2以上版本的nginx,補丁爲check_1.2.1+.patch)
patch -p1 < /usr/local/src/nginx_upstream_check_module-master/check_1.2.1.patch
開始編譯安裝nginx:
### 注意下面的--with-pcre=pcre的安裝包路徑千萬不要搞錯了。
./configure --user=www --group=www --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.21 --lock-path=/var/run/nginx.lock --pid-path=/var/run/nginx.pid --add-module=/usr/local/src/nginx_upstream_check_module-master
make && make install
添加nginx配置:
因爲篇幅大小原因,這個配置文件是省略了部分常用內容,但是並不代表其他不需要.(以下爲需要特別注意的地方)
upstream www { #ip_hash; server 192.168.100.11:80; #多個realserver服務器在此添加 server 192.168.100.12:80; check interval=3000 rise=2 fall=5 timeout=1000; } server { listen 80; server_name www.etiantian.org; #charset koi8-r; access_log logs/host.access.log main; root /data0/www; location / { proxy_pass http://www; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /status { check_status; access_log off; #allow SOME.IP.ADD.RESS; #deny all; } }
※ 附件會發上來我的nginx 配置文件,希望對大家有幫助.
1 upstream 後面的www不是域名.可以根據需要自己起。
2
upstream 後面的www 要和proxy_pass http://www; 裏面的www相互對應.
3 註解:check interval=3000 rise=2 fall=5 timeout=1000;
interval檢測間隔時間,單位爲毫秒。
rsie請求2次正常的話,標記此realserver的狀態爲up。
fall表示請求5次都失敗的情況下,標記此realserver的狀態爲down。
timeout爲超時時間,單位爲毫秒。
測試nginx:
/usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
啓動nginx:
/usr/local/nginx/sbin/nginx &
測試截圖: