Nginx returns 502 如果服务在启动时未就绪

Nginx returns 502 if service not ready on start

我正在使用 Raspberry Pi 构建的网络摄像头。它具有以下组件:

我遇到的问题是,如果 FFserver 在 Nginx 启动时未在端口 8090 上完全准备好,它将持续 return stream.mjpeg 的 502,即使流是 运行。就好像 Nginx 确定主机不存在,然后再也不会尝试一样。一旦我 restart/reload Nginx 配置它再次开始工作。

为什么会这样?是否缺少重试条件?

这是 Nginx 的配置:

server {
  listen 80;
  rewrite ^ https://$host$request_uri? permanent;
}

server {
  listen 443 ssl;

  ssl_certificate /etc/nginx/ssl/nginx.crt;
  ssl_certificate_key /etc/nginx/ssl/nginx.key;

  location / {
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_next_upstream error timeout http_502;

    # Basic auth
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/htpasswd;
  }

  location /stream.mjpeg {
    proxy_pass http://localhost:8090/stream.mjpeg;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_next_upstream error timeout http_502;

    # Basic auth
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/htpasswd;
  }
}

/var/log/nginx/error.log 查看 Nginx 日志后,我可以看到请求发送到 IPv6 环回地址而不是 IPv4。我将 localhost 更改为 127.0.0.1,问题已解决。为什么这会在重新启动 Nginx 之后而不是之前起作用,这对我来说仍然是个谜。

  location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_next_upstream error timeout http_502;

    # Basic auth
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/htpasswd;
  }