1. 程式人生 > >nginx健康檢查和主備切換

nginx健康檢查和主備切換

1、參考部落格

https://blog.csdn.net/moqiang02/article/details/42846221

2、使用第一種方式注意點

在主備切換的方式下需要指定備用節點

upstream mysvr {
	server 192.168.0.104:8080;
	server 192.168.0.104:8081 backup;
    }
location /examples {
           # root   html;
            index  index.html index.htm;
	    proxy_pass  http://mysvr;
        }

proxy_connect_timeout 、proxy_read_timeout、max_fails=1 fail_timeout=10s可以根據具體情況設定

3、使用第二種方式需要安裝第三方模組

安裝nginx的命令:

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
[[email protected] src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
[
[email protected]
src]# tar zxvf pcre-8.35.tar.gz
[[email protected] src]# cd pcre-8.35
[[email protected] pcre-8.35]# ./configure
[[email protected] pcre-8.35]# make && make install
[[email protected] pcre-8.35]# pcre-config --version
[[email protected] src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
[[email protected] src]# tar zxvf nginx-1.6.2.tar.gz
[[email protected] src]# cd nginx-1.6.2
[[email protected] nginx-1.6.2]# ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
[[email protected] nginx-1.6.2]# make
[[email protected] nginx-1.6.2]# make install
[[email protected] nginx-1.6.2]# /usr/local/webserver/nginx/sbin/nginx -v

安裝 nginx_upstream_check_module模組

[[email protected] ~]# cd /usr/local/src
[[email protected] src]# wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
[[email protected] src]# unzip master
[[email protected] src]# ll -d nginx_upstream_check_module-master
drwxr-xr-x. 6 root root 4096 Dec  1 02:28 nginx_upstream_check_module-master

為nginx打補丁

[[email protected] /usr/local/src]# cd nginx-1.6.2 # 進入nginx的原始碼目錄
[[email protected] nginx-1.6.2]# patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch
[[email protected] nginx-1.6.2]# ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35 --add-module=../nginx_upstream_check_module-master/(編譯引數需要和之前的一樣)
[[email protected] nginx-1.6.2]make (注意:此處只make)
[[email protected] nginx-1.6.2]# ps -ef|grep nginx(檢視nginx程序)
[[email protected] nginx-1.6.2]# kill -9 1523(上面程序)
[[email protected] nginx-1.6.2]# cp ./objs/nginx /usr/local/webserver/nginx/sbin/

修改/usr/local/webserver/nginx/conf/nginx.conf

 upstream mysvr {
	server 192.168.0.104:8080;
	server 192.168.0.104:8081 backup;
	check interval=3000 rise=2 fall=5 timeout=1000 type=http;
    }
location /examples {
           # root   html;
            index  index.html index.htm;
	    proxy_pass  http://mysvr;
        }
	location /nstatus {
         check_status;
         access_log off;
         #allow IP;
         #         #deny all;
         #               
          }
[[email protected] nginx-1.6.2]# /usr/local/webserver/nginx/sbin/nginx -s reload

訪問

http://localhost/examples/index.html

關閉8080tomcat再次訪問

訪問http://localhost/nstatus?format=json

注意:

1、主要定義好type。由於預設的type是tcp型別,因此假設你服務啟動,不管是否初始化完畢,它的埠都會起來,所以此時前段負載均衡器為認為該服務已經可用,其實是不可用狀態。

2、注意check_http_send值的設定。由於它的預設值是"GET / HTTP/1.0\r\n\r\n"。假設你的應用是通過http://ip/name訪問的,那麼這裡你的check_http_send值就需要更改為"GET /name HTTP/1.0\r\n\r\n"才可以。針對採用長連線進行檢查的,這裡增加keep-alive請求頭,即"HEAD /name HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"。如果你後端的tomcat是基於域名的多虛擬機器,此時你需要通過check_http_send定義host,不然每次訪問都是失敗,範例:check_http_send "GET /mobileapi HTTP/1.0\r\n HOST www.redhat.sx\r\n\r\n";