一、參考博客html
https://blog.csdn.net/moqiang02/article/details/42846221nginx
二、使用第一種方式注意點c++
在主備切換的方式下須要指定備用節點git
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能夠根據具體狀況設置github
三、使用第二種方式須要安裝第三方模塊web
安裝nginx的命令:json
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
[root@luculent src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
[root@luculent src]# tar zxvf pcre-8.35.tar.gz
[root@luculent src]# cd pcre-8.35
[root@luculent pcre-8.35]# ./configure [root@luculent pcre-8.35]# make && make install
[root@luculent pcre-8.35]# pcre-config --version
[root@luculent src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
[root@luculent src]# tar zxvf nginx-1.6.2.tar.gz
[root@luculent src]# cd nginx-1.6.2
[root@luculent 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 [root@luculent nginx-1.6.2]# make [root@luculent nginx-1.6.2]# make install
[root@bogon nginx-1.6.2]# /usr/local/webserver/nginx/sbin/nginx -v
安裝 nginx_upstream_check_module模塊後端
[root@luculent ~]# cd /usr/local/src [root@luculent src]# wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master [root@luculent src]# unzip master [root@luculent 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打補丁api
[root@luculent /usr/local/src]# cd nginx-1.6.2 # 進入nginx的源碼目錄 [root@luculent nginx-1.6.2]# patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch [root@luculent 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/(編譯參數須要和以前的同樣) [root@luculent nginx-1.6.2]make (注意:此處只make) [root@luculent nginx-1.6.2]# ps -ef|grep nginx(查看nginx進程) [root@luculent nginx-1.6.2]# kill -9 1523(上面進程) [root@luculent nginx-1.6.2]# cp ./objs/nginx /usr/local/webserver/nginx/sbin/
修改/usr/local/webserver/nginx/conf/nginx.conftomcat
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; # }
[root@luculent nginx-1.6.2]# /usr/local/webserver/nginx/sbin/nginx -s reload
訪問
http://localhost/examples/index.html
關閉8080tomcat再次訪問
訪問http://localhost/nstatus?format=json
注意:
一、主要定義好type。因爲默認的type是tcp類型,所以假設你服務啓動,無論是否初始化完畢,它的端口都會起來,因此此時前段負載均衡器爲認爲該服務已經可用,實際上是不可用狀態。 二、注意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";