[Nginx] Load balancing when one of the servers goes down

If we build a simple load balancing, then shut down one of the servers and access it again, we will find that our browser is stuck and spinning in circles, and it will take a long time for the results to be displayed. From this we can conclude that if one of the services hangs up when Nginx is under load, it will forward the request to another machine that can provide services, and the default timeout wait between them is 60s. We can change this time through this configuration and proxy_connect_timeoutdefine the timeout for establishing a connection with the proxy server.

Note that this timeout usually cannot exceed 75 seconds.

    upstream cluster{
    	server nacos-node1.strongman.cluster.local:8848;
        server nacos-node2.strongman.cluster.local:8848;
        server nacos-node3.strongman.cluster.local:8848;
        server nacos-node4.strongman.cluster.local:8848;
    }

    server {
        listen       80;
        server_name  localhost;
        location / { 
          proxy_pass http://cluster;
          proxy_set_header Host $http_host;
          
          # 添加 HTTP 响应头,以便知道负载到哪台服务器上
          add_header backendIP $upstream_addr; 
          # 响应码
          add_header backendCode $upstream_status; 
          
          # 服务器与被代理服务连接超时时间,代理超时
          proxy_connect_timeout 60s;
        } 
    }

Reference configuration

Module ngx_http_proxy_module

Guess you like

Origin blog.csdn.net/u010638673/article/details/132582536
Recommended