如果日志写的是 connect() failed (111: Connection refused),那更直接:后端压根没监听,或者监听在别的端口,或者防火墙拦了。还有一种 no live upstreams while connecting to upstream,说明健康检查认为所有后端都死了,Nginx 手里没牌可打。
504 呢?常见 upstream timed out (110: Connection timed out) while reading response header from upstream。注意这个“while reading response header”,意思是 Nginx 等后端返回响应头,等超时了。proxy_read_timeout 默认 60 秒。有人一看 504 就骂后端慢,其实也可能是网络丢包、DNS 解析慢、上游连接池耗尽。别急着下结论。