当前位置: 首页 > news >正文

nginx upstream转发连接错误情况研究

本次测试用到3台服务器:

192.168.10.115:转发服务器A

192.168.10.209:upstream下服务器1

192.168.10.210:upstream下服务器2

1台客户端:192.168.10.112

服务器A中nginx主要配置如下:

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';keepalive_timeout  65;#gzip  on;upstream testup{server 192.168.10.209 weight=1 max_fails=1 fail_timeout=30s;server 192.168.10.210 weight=1 max_fails=1 fail_timeout=30s;}server {listen       80;server_name  localhost;#charset koi8-r;access_log  logs/host.access.log  main;location / {#root   html;#index  index.html index.htm;proxy_next_upstream http_502 http_504 error timeout invalid_header;proxy_ignore_client_abort on;proxy_send_timeout 60s;proxy_read_timeout 300s;proxy_next_upstream_tries 0;proxy_pass http://testup;proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP $remote_addr;proxy_connect_timeout 3;proxy_redirect default;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

nginx工作进程配置为2,

服务器1和服务器2都是普通的web服务配置不在此展示了

服务器1页面1:

服务器2页面2:

正常访问服务器A会在如上页面1和2之间切换

测试情况1:关闭服务器1

请求A地址,先卡顿差不多3s(应该与'proxy_connect_timeout 3'相关)然后切换成页面2内容,随后刷新无任何卡顿,30s(nginx相关配置:'max_fails=1 fail_timeout=30s')过后再次请求仍会卡顿3s成功随后并不卡顿,nginx报错日志如下:

2024/09/21 18:23:19 [error] 6056#0: *114 upstream timed out (110: Connection timed out) while connecting to upstream, client: 192.168.10.112, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.10.209:80/", host: "192.168.10.115"

 测试情况2:不关闭服务器1,只关闭其nginx服务

请求A地址,不出现卡顿,页面一直显示的页面2内容。nginx报错日志:

2024/09/21 18:30:14 [error] 6055#0: *133 connect() failed (113: No route to host) while connecting to upstream, client: 192.168.10.112, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.10.209:80/", host: "192.168.10.115"
2024/09/21 18:30:48 [error] 6055#0: *133 connect() failed (113: No route to host) while connecting to upstream, client: 192.168.10.112, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.10.209:80/", host: "192.168.10.115"

 此处说明'proxy_connect_timeout 3'针对的是请求能否转达服务器,与目标服务器上的nginx服务是否正常运行无关。

 测试情况3:关闭服务器1和2(2选择的是屏蔽了对外端口)

请求A地址,卡顿了差不多6,7s出现如下页面:

 此处应该是先后请求了两个服务器耗时3*2s,随后请求不卡顿直接返回如上页面,30s后继续请求仍会卡顿6s随后不卡顿。nginx报错日志:

 此处前两条分别是两台服务器的超时日志,后续的日志“no live upstreams while connecting to upstream”是nginx在由于之前请求超时已经判断两台机器都不可用,在30s不会再去转发请求到该服务器,没有了可用的upstreams直接报该错误。

测试情况4:模拟慢网情况

恢复服务器1,2的正常访问,保证访问A地址能正常在页面1,2之前切换。

模拟网络延迟:

tc qdisc add dev ens33 root netem delay 1000ms

 参考文档:1分钟学会在Linux下模拟网络延迟_linux模拟网络延迟-CSDN博客

这里将服务器A的nginx配置'proxy_connect_timeout'值改为1。如果喜欢等待的老铁也可以选择不改,但上面的延迟时间要改为3000ms了。这延迟那真的是连xshell连接服务器的输入输出都延迟了╮(╯▽╰)╭,只要是走网卡的应该都会被卡一下。

此时访问A地址,卡顿了一段时间后页面如下:

 与之前的关闭服务器1,2的情况大致一样。首次卡顿了4,5s返回如上页面,随后刷新2s返回,过30s后仍会卡顿4,5s返回。错误日志输出情况也与情况3一致。

将服务器A的nginx配置'proxy_connect_timeout'值改为2。继续请求A地址,差不多4,5s返回页面1或页面2,并且页面内容正常切换。

测试情况4.2:调整proxy_next_upstream_tries

在情况4的弱网请求失败情况下,修改'proxy_next_upstream_tries'值为1,请求A地址3次。nginx错误日志:

2024/09/21 21:03:10 [notice] 6399#0: signal process started
2024/09/21 21:03:29 [error] 6401#0: *396 upstream timed out (110: Connection timed out) while connecting to upstream, client: 192.168.10.112, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.10.209:80/", host: "192.168.10.115"
2024/09/21 21:03:40 [error] 6401#0: *396 upstream timed out (110: Connection timed out) while connecting to upstream, client: 192.168.10.112, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.10.210:80/", host: "192.168.10.115"
2024/09/21 21:03:49 [error] 6401#0: *396 no live upstreams while connecting to upstream, client: 192.168.10.112, server: localhost, request: "GET / HTTP/1.1", upstream: "http://testup/", host: "192.168.10.115

修改'proxy_next_upstream_tries'值为2,请求A地址2次,错误日志:

2024/09/21 21:23:16 [error] 6432#0: *413 upstream timed out (110: Connection timed out) while connecting to upstream, client: 192.168.10.112, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.10.209:80/", host: "192.168.10.115"
2024/09/21 21:23:17 [error] 6432#0: *413 upstream timed out (110: Connection timed out) while connecting to upstream, client: 192.168.10.112, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.10.210:80/", host: "192.168.10.115"
2024/09/21 21:23:22 [error] 6432#0: *413 no live upstreams while connecting to upstream, client: 192.168.10.112, server: localhost, request: "GET / HTTP/1.1", upstream: "http://testup/", host: "192.168.10.115"

 相较于上次修改配置,这次请求第一次就连续尝试连接209和210,都失败,归为不可用,第二次请求直接返回“no live upstreams”报错。

再次修改'proxy_next_upstream_tries'值为3,请求A地址2次,错误日志:

2024/09/21 21:28:59 [error] 6446#0: *423 upstream timed out (110: Connection timed out) while connecting to upstream, client: 192.168.10.112, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.10.209:80/", host: "192.168.10.115"
2024/09/21 21:29:00 [error] 6446#0: *423 upstream timed out (110: Connection timed out) while connecting to upstream, client: 192.168.10.112, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.10.210:80/", host: "192.168.10.115"
2024/09/21 21:29:12 [error] 6446#0: *423 no live upstreams while connecting to upstream, client: 192.168.10.112, server: localhost, request: "GET / HTTP/1.1", upstream: "http://testup/", host: "192.168.10.115"

跟上次测试日志输出一样,upstream下总共就2台服务器,不会因为proxy_next_upstream_tries的值大于2就循环再次请求第一个服务器。所以proxy_next_upstream_tries的值大于转发的服务器的数量时以服务器数量为准。proxy_next_upstream_tries值为0的时候从日志上看应该是按照服务器数量尝试连接的。

测试情况4.3:调整max_fails

配置nginx中max_fails值改为2,请求地址A3次,前两次请求时间明显较长,报错日志:

30s内每个upstream下服务器连接超时2次,才都判定为不可用时,最后一次请求出现“no live upstream”。

至此,想要测试的差不多结束了。

最后测试完记得删除网络延迟:

tc qdisc del dev ens33 root

如果上述测试对您有学习和工作有所帮助就点个赞吧!

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Python | Leetcode Python题解之第429题N叉树的层序遍历
  • Leetcode 每日一题:Diameter of Binary Tree
  • TS React 项目中使用TypeScript
  • 串的存储实现方法(与链表相关)
  • Pybullet 安装过程
  • 面试干货|自动化测试中常见面试题
  • 摆脱困境并在iPhone手机上取回删除照片的所有解决方案
  • 神经网络面试题目
  • Get请求-LocalDateTime的转换问题
  • Python在数据科学与机器学习中的应用
  • 大话Python|基础语法(上)
  • 【监控】【Nginx】使用 Docker 部署 ELK Stack 监控 Nginx
  • Vite + Vue + TypeScript 项目搭建总结
  • 【ComfyUI】自定义节点ComfyUI_LayerStyle——模仿 Adob​​e Photoshop 的图层样式、图层混合、图文混合、添加不可见水印
  • 安卓13去掉下拉菜单的Dump SysUI 堆的选项 android13删除Dump SysUI 堆
  • SegmentFault for Android 3.0 发布
  • 「译」Node.js Streams 基础
  • C++类的相互关联
  • JavaScript中的对象个人分享
  • Java教程_软件开发基础
  • java取消线程实例
  • JS+CSS实现数字滚动
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • MySQL的数据类型
  • Quartz初级教程
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • session共享问题解决方案
  • 不发不行!Netty集成文字图片聊天室外加TCP/IP软硬件通信
  • 程序员该如何有效的找工作?
  • 后端_ThinkPHP5
  • 两列自适应布局方案整理
  • 每天一个设计模式之命令模式
  • 强力优化Rancher k8s中国区的使用体验
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 入门级的git使用指北
  • 使用 QuickBI 搭建酷炫可视化分析
  • 我的面试准备过程--容器(更新中)
  • 7行Python代码的人脸识别
  • 阿里云API、SDK和CLI应用实践方案
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • ​zookeeper集群配置与启动
  • (3)(3.5) 遥测无线电区域条例
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • (ISPRS,2021)具有遥感知识图谱的鲁棒深度对齐网络用于零样本和广义零样本遥感图像场景分类
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (四)事件系统
  • (原+转)Ubuntu16.04软件中心闪退及wifi消失
  • (转) ns2/nam与nam实现相关的文件
  • *p=a是把a的值赋给p,p=a是把a的地址赋给p。
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版
  • .NET CORE Aws S3 使用
  • .net core webapi 部署iis_一键部署VS插件:让.NET开发者更幸福