最近遇到Proxy服务中两个非常常见的问题,但还没有研究透彻,所以今天只描述问题,没有结论。
下图是Proxy服务模式,大概的配置模式如下:
error_log /var/log/error.log notice ;
log_format upstreamlog '[$time_local] $remote_addr - $remote_user - $server_name $host to: $upstream_addr: $request $status upstream_response_time $upstream_response_time msec $msec request_time $request_time';
server {
listen [::]:8081;
server_name _;
access_log /var/log/access.log upstreamlog;
location / {
proxy_pass https://api.test.com;
proxy_ssl_server_name on;
proxy_read_timeout 160;
}
}
上述的配置有两个技巧,首先就是配置log格式,可以打印upstream的请求时间,这对于debug非常有用;第二个技巧就是日志级别是notice,可以打印debug、warn、error等日志,排查的时候信息更丰富。
接下去回到正题:
第一个报错(no live upstreams):
2023/11/03 16:24:01 [error] 17264#17264: *47015 no live upstreams while connecting to upstream
2023/11/03 16:24:11 [error] 17264#17264: *47015 no live upstreams while connecting to upstream
这两条错误的ID是一样的,是在一个请求中产生的。
这个请求的access日志如下:
[03/Nov/2023:16:24:11 +0800] 121.41.21.217 - - - _ to: api.test.com: POST / HTTP/1.1 502 upstream_response_time 0.000 msec 1698999851.529 request_time 0.000
[03/Nov/2023:16:24:11 +0800] 121.41.12.217 - - - _ 41.251.45.171 to: api.test.com: POST / HTTP/1.1 502 upstream_response_time 0.000 msec 1698999851.674 request_time 0.000
产生了一个502错误,502 Bad Gateway:
502 Bad Gateway 是一种 HTTP 协议的服务端错误状态代码,它表示作为网关或代理的服务器,从上游服务器中接收到的响应是无效的
注意upstream_response_time显示为0秒。
第二个报错upstream timed out:
2023/11/03 18:28:29 [warn] 17264#17264: *48081 upstream server temporarily disabled while reading response header from upstream, client
2023/11/03 18:28:29 [error] 17264#17264: *48081 upstream timed out (110: Connection timed out) while reading response header from upstream
其中有一个warn报错,对应的access日志:
[03/Nov/2023:18:28:29 +0800] 121.41.23.217 - - - _ 47.251.45.271 to: 104.18.7.192:443: POST / HTTP/1.1 504 upstream_response_time 160.018 msec 1699007309.802 request_time 160.018
出现了一个504错误,504 Gateway Timeout:
504 Gateway Timeout 是一种 HTTP 协议的服务器端错误状态代码,表示扮演网关或者代理的服务器无法在规定的时间内获得想要的响应
今天就说到这儿,后面详细分析产生这两个问题的原因。