我运行一个家庭服务器,其中 nginx 反向代理到 Node.js/PM2 上游。通常情况下它工作得很好。然而,当我想要进行更改时,我运行pm2 reload pnameor pm2 restart pname,这会导致 nginx502 Bad Gateway在找到新的上游之前抛出大约 10-20 秒。
我的 Node.js 应用程序启动速度非常快,我 99% 确信上游启动并绑定到端口实际上并不需要那么长时间(当我不使用 nginx 层时,它可以立即访问)。如何消除 nginx 解决问题所需的额外时间?
来自 nginx/error.log:
2021/01/29 17:50:35 [error] 18462#0: *85 no live upstreams while connecting to upstream, client: [ip], server: hostname.com, request: "GET /path HTTP/1.1", upstream: "http://localhost/path", host: "www.hostname.com"
Run Code Online (Sandbox Code Playgroud)
从我的 nginx 域配置:
server {
listen 80;
server_name hostname.com www.hostname.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name hostname.com www.hostname.com;
# ...removed ssl stuff...
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
gzip_proxied no-cache no-store private expired auth;
gzip_min_length 1000;
location / {
proxy_pass http://localhost:3010;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_read_timeout 240s;
}
}
Run Code Online (Sandbox Code Playgroud)
这是由上游的默认行为引起的,这可能并不明显,因为您没有使用该upstream指令明确声明上游。您的指令配置upstream将如下所示:
upstream backend {
server localhost:3010;
}
...
server {
listen 443 ssl;
...
location / {
proxy_pass http://backend;
...
}
}
Run Code Online (Sandbox Code Playgroud)
在这种形式中,很明显您只是依赖于指令的默认选项server。该server指令有很多选项,但其中两个很重要:max_fails和fail_timeout。这些选项控制失败状态以及 nginx 应如何处理它们。默认情况下max_fails=1,fail_timeout=10 seconds这意味着在尝试与上游 nginx 通信失败后,将等待 10 秒,然后再次尝试。
为了避免在您的环境中出现这种情况,您可以通过设置来禁用此机制max_fails=0:
upstream backend {
server localhost:3010 max_fails=0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1700 次 |
| 最近记录: |