Home  >  Q&A  >  body text

How to achieve high availability with nginx load balancing

Use nginx to implement simple load balancing, the configuration is as follows


...

http{
    ...
    
    upstream mytomcatscluster{
        #ip_hash 在同一台服务器部署了2个tomcat,使用不同的端口
        server 127.0.0.1:8080 weight=2;
        server 127.0.0.1:8180 weight=1;
    }
    
    ...
    
    server{
        ...
        
        # 将所有后端请求分发到我定义的集群服务器上
        location /service/fen/{
            proxy_pass http://mytomcatscluster;
        }
        
        ...
    }
}

...

The current problem is: If both tomcats are normal, nginx can distribute requests to the specified server normally. Using polling, the weight can also work, but if I give one of the tomcats If it is turned off, the request will be stuck, because there will be requests distributed by nginx to the failed server. How to deal with this situation? Does ngxin have a corresponding solution strategy? Is there a way for nginx to monitor the running status of cluster servers? How to configure?

天蓬老师天蓬老师2687 days ago727

reply all(2)I'll reply

  • 仅有的幸福

    仅有的幸福2017-06-10 09:51:33

    upstream has two parameters max_fails and fail_timeout. If a certain server has max_fails connection failures within the fail_timeout time, then Nginx will think that it has hung up and will not request it during the fail_timeout time.
    fail_timeout defaults to 10s, max_fails defaults to 1.

    In addition, there is a third-party module nginx_upstream_check_module specially used to detect the health of the backend server
    .
    yaoweibin/nginx_upstream_check_module: Health checks upstreams for nginx

    https://github.com/yaoweibin/...

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-10 09:51:33

    No need to use ip_hash to store the session in redis

    reply
    0
  • Cancelreply