Nginx本身并没有提供一个health check的机制
但在upstream中可以通过配置max_fails和fail_timeout以及proxy_next_upstream这些参数,见之前的这个文章
可以实现,当某个server挂掉后,在多长时间内不再向该server发请求了
但过了那个时间后,nginx仍然后继续给server发请求,如果失败后,则
在error的日志中,我们是能看到类似这样的错误日志的
2011/11/11 16:36:18 [error] 11834#0: *51 connect() failed (111: Connection refused) while connecting to upstream, client: 10.10.79.108
nginx默认是会将该失败的请求再转向另外一个server去处理,所以理论上讲应该不会影响Client端
这样的方式并不是一个真正的健康检测机制,不过可以通过一个第三方的插件healthcheck_nginx_upstreams可以实现这个功能
这个插件貌似现在还有Bug,需要打https://github.com/liseen/healthcheck_nginx_upstreams这么一个patch,然后就能在round robin下实现健康检查了
安装很简单
1 2 3 4 5 |
cd nginx-1.0.9 patch -p1 < cep21-healthcheck_nginx_upstreams-8870d34/healthcheck.patch ./configure --add-module=./cep21-healthcheck_nginx_upstreams-8870d34 |
配置如下参数即可
healthcheck_enabled;
healthcheck_delay 1000;
healthcheck_timeout 2000;
healthcheck_failcount 3;
healthcheck_send "GET /error.jsp HTTP/1.0" 'Host: xxxx.xxx.com';
当Down掉后台的某台server后,则nginx不会把请求再转发到这台server上,所以error日志中也不会有upstream错误的日志了
可以在nginx里配置一个location,通过web界面去查看health check的状态
location ~ /health {
healthcheck_status;
access_log off;
allow 10.1.1.0/24;
deny all;
}
十月 31st, 2011Nginx的proxy_set_header继承
nginx配置中,proxy_set_header可以放在http,server,loaction中
逻辑上讲,下面loaction的配置应该会继承上面的proxy_set_header
但Nginx有一个限制
proxy_set_header
directives issued at higher levels are only inherited when no proxy_set_header
directives have been issued at a given level.
就是说,当location里写的有其它的proxy_set_header时,则无法继承高层server中定义的proxy_set_header的设置
如果location想继承上面定义的proxy_set_header,则location里不能proxy_set_header指令
六月 7th, 2011用proxy_redirect解决proxy后重定向url问题
今天配置Nginx时,遇到了“/”的问题
前端的Nginx负责把http://mtpc.sohu.com/yum/Server/开头的url反向代理到后端的http://192.168.1.1/Server/上
对于有完整的路径,如http://mtpc.sohu.com/yum/Server/的代理没有问题,Server对应后台服务器的一个目录
但当访问http://mtpc.sohu.com/yum/Server时,后端Nginx会发送一个301到/上,于是返回到前端后URL变成了
http://mtpc.sohu.com/Server/,这个url显然不是我们想要的
在Apache中有个ProxyPassReverse的参数,用来调整反向代理服务器发送的http应答头的url,可以解决这个问题
查了Nginx的手册后,终于发现了proxy_redirect这个参数,它实现的功能和ProxyPassReverse类似
增加如下配置:
location ^~ /yum
{
proxy_pass http://192.168.1.1/;
proxy_redirect http://mtpc.sohu.com/ /yum/;
}
这样,当访问http://mtpc.sohu.com/yum/Server后,就会301到http://mtpc.sohu.com/yum/Serve