nginx配置web容器实现自动剔除异常(失败)的服务
要实现nginx配置web容器实现自动剔除异常的服务,主要在nginx的conf下的nginx.conf文件的upstream中配置信息。
nginx负载均衡可以参考文章:https://www.52jingya.com/aid9.html
nginx配置web容器实现自动剔除异常(失败)的服务
具体内容如下:
upstream my_server {
server 127.0.0.1:8080 weight=1 max_fails=1 fail_timeout=60s;
server 127.0.0.1:8088 weight=1 max_fails=1 fail_timeout=60s;
server 127.0.0.1:8086 weight=7 max_fails=1 fail_timeout=60s down;
server 127.0.0.1:8087 weight=7 max_fails=1 fail_timeout=60s backup;
}
说明:
上面配置的是同一台服务器,负载了2个tomcat容器,端口分别为8080和8088。请求失败1次后,服务剔除60s(在60s内该服务不会被请求到)。
参数说明:
- down 表示单前的server暂时不参与负载
- weight 默认为1.weight越大,负载的权重就越大。
- max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
- fail_timeout:max_fails次失败后,暂停的时间。
- backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
下面为nginx.conf的完整配置
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 45;
#gzip on;
upstream my_server {
server 127.0.0.1:8080 weight=7 max_fails=1 fail_timeout=60s;
server 127.0.0.1:8088 weight=7 max_fails=1 fail_timeout=60s;
server 127.0.0.1:8086 weight=7 max_fails=1 fail_timeout=60s down;
server 127.0.0.1:8087 weight=7 max_fails=1 fail_timeout=60s backup;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ~ .*\.(jsp|syy|do|html)$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://my_server;
client_max_body_size 5m;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}