一、参考博客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";