【Nginx】负载均衡当其中一台服务器宕机之后

搭建一个简单的负载均衡,然后关闭其中一台再来访问,会发现我们的浏览器卡住一直转圈圈,过了很久才会显示结果。由此我们可以得出结论Nginx负载的时候如果其中一台服务挂掉了,它会把请求转发到另一个可以提供服务的机器,其中这之间的超时等待默认是60s。我们可以通过这一个配置去改变这个时间,proxy_connect_timeout 定义与代理服务器建立连接的超时。

请注意,此超时通常不能超过75秒。

复制代码
    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;
        } 
    }

参考配置

Module ngx_http_proxy_module

相关推荐
还是大剑师兰特2 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
xing-xing2 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
PC2005-cloud2 天前
Nginx 防盗链配置实战:用 referer 模块保护网站静态资源
nginx
PHP实战开发录2 天前
Nginx上传大文件为什么报413
nginx·php·开发
Lsetea3 天前
Nginx报No required SSL certificate was sent:mTLS客户端证书排查
运维·nginx·https·ssl·openssl
szephyr4 天前
HTTPS 证书申请与自动续期:acme.sh + Let‘s Encrypt 完整记录
nginx·https·acme.sh·证书续期
Lsetea4 天前
部署证书后提示“证书名称与输入不匹配”:Safari 报错的三层排查
nginx·https·safari·ssl证书·openssl
打工仔折腾 AI5 天前
FastAPI 从本机到生产服务器:Nginx+Gunicorn+Uvicorn 完整部署实录
人工智能·后端·python·nginx·fastapi·gunicorn
Linux运维技术栈5 天前
如何避免绕过CDN的攻击?基于 CDN 回源请求头鉴权的防御实操与安全性剖析
nginx·安全·cdn
Lsetea5 天前
OpenSSL快速生成域名证书:含SAN的自签命令、验证与排错
nginx·https·ssl证书·openssl·自签证书