在一次业务中客户端请求osb平台再经过nginx转发后端,开发反馈请求次数大于1导致问题,经排查客户端请求一次,osb平台设置超时为30s,nginx配置等待上游服务器响应时最多等待30秒
部分配置文件
1 2 3 4 5 6 7 8 9 10 11
| upstream xx { server 10.6.6.1:8080 weight=1; server 10.6.6.2:8080 weight=1; server 10.6.6.45:8080 weight=1; } location / { root html; proxy_pass http: proxy_set_header X-Forwarded-For $remote_addr; proxy_read_timeout 30; }
|
1 2 3 4 5 6
| # access.log日志
10.6.6.35|18/Jun/2024:10:35:42 +0800|POST /nis/static/inv HTTP/1.1|499|0|-|-|10.6.6.1:8080, 10.6.6.2:8080|504, -|33.730|30.001, - 10.6.6.37|18/Jun/2024:10:42:39 +0800|POST /nis/static/inv HTTP/1.1|200|1006|-|-|10.6.6.1:8080|200|1.151|1.151 10.6.6.37|18/Jun/2024:10:47:19 +0800|POST /nis/static/inv HTTP/1.1|200|846|-|-|10.6.6.2:8080|200|0.595|0.595 10.6.6.36|18/Jun/2024:11:15:21 +0800|POST /nis/static/inv HTTP/1.1|499|0|-|-|10.6.6.45:8080, 10.6.6.1:8080|504, -|33.170|30.000, -
|
1 2 3 4
| #error.log日志
2024/06/18 10:35:38 [error] 12425#0: *8490383024 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 10.6.6.35, server: , request: "POST /nis/static/inv HTTP/1.1", upstream: "http://10.6.6.1:8080/nis/static/inv", host: "10.6.6.5" 2024/06/18 11:15:18 [error] 12422#0: *8490923679 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 10.6.6.36, server: , request: "POST /nis/static/inv HTTP/1.1", upstream: "http://10.6.6.45:8080/nis/static/inv", host: "10.6.6.5"
|
访问日志显示2个时间节点请求时出现499状态码然后请求了2次后端节点
proxy_next_upstream
经查找默认值: proxy_next_upstream error timeout会开启,即使在为配置情况下包括以下
| 状态码 |
含义 |
| error |
当nginx无法与上游服务器建立连接时 |
| timeout |
当上游服务器在规定时间内没有响应时 |
| invalid_header |
当上游服务器返回无效的响应头时 |
| http_500 |
当上游服务器返回500内部服务器错误时 |
| http_502 |
当上游服务器返回502错误网关时 |
| http_503 |
当上游服务器返回503服务不可用时 |
| http_504 |
当上游服务器返回504网关超时时 |
| off |
停止将请求发送给下一台后端服务器 |
默认当nginx与上游超时则会轮询upstream中所包含所有的节点再进行请求
自定义指定
根据项目需求自定义那种情况下应该转发到下一个上游服务器
语法: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_404 | off …;
1 2 3 4 5 6 7
| location / { proxy_pass http: proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_read_timeout 30; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; }
|