简体   繁体   English

如果服务在启动时未准备好,Nginx 返回 502

[英]Nginx returns 502 if service not ready on start

I'm working on a webcam I built with a Raspberry Pi.我正在研究用 Raspberry Pi 构建的网络摄像头。 It has these components:它具有以下组件:

  • Node.js on port 3000端口 3000 上的 Node.js
  • FFmpeg / FFserver on port 8090端口 8090 上的 FFmpeg / FFserver
  • Nginx in front to proxy these services together over ports 80/443 (HTTP/HTTPS)前面的 Nginx 通过端口 80/443 (HTTP/HTTPS) 将这些服务代理在一起

The problem that I am having is that if FFserver is not fully ready on port 8090 when Nginx starts up it will continually return a 502 for stream.mjpeg , even though the stream is running.我遇到的问题是,如果 Nginx 启动时 FFserver 未在端口 8090 上完全准备好,即使流正在运行,它也会持续为stream.mjpeg返回 502 。 It's as if Nginx determines that the host does not exist and then never tries again.就好像 Nginx 确定主机不存在然后再也不尝试一样。 Once I restart/reload the Nginx config it starts working again.一旦我重新启动/重新加载 Nginx 配置,它就会再次开始工作。

Why does this happen?为什么会发生这种情况? Is there a retry condition I am missing?是否有我缺少的重试条件?

Here is the config for 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;
  }
}

After reviewing the Nginx logs at /var/log/nginx/error.log I could see that the requests were going to the IPv6 loopback address and not the IPv4.查看/var/log/nginx/error.log的 Nginx 日志后,我可以看到请求将发送到 IPv6 环回地址而不是 IPv4。 I changed localhost to 127.0.0.1 and the issues were resolved.我将localhost更改为127.0.0.1并解决了问题。 It's still a mystery to me as to why this would work after restarting Nginx but not before.为什么这会在重新启动 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;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nginx 在 Kubernetes 中返回 Bad Gateway 502 - Nginx returns Bad Gateway 502 in Kubernetes nginx在DigitalOcean上返回“错误的网关-502” - nginx returns “Bad gateway - 502” on DigitalOcean Nginx 向浏览器返回 502 但与 curl 一起工作正常 - Nginx returns 502 to browsers but works fine with curl 当php-fpm返回500时,Nginx返回502错误 - Nginx returns 502 error when php-fpm returns 500 当HTTP请求返回502状态代码时,Nginx返回“ 502 Bad GateWay” + requestUrl - Nginx returns “502 Bad GateWay” + requestUrl when HTTP request returns 502 status code Laravel 5:雄辩的选择+在Nginx / Apache上返回错误502 - Laravel 5: Eloquent select + with returns error 502 on nginx/apache 使用Nginx向Node.js进行反向代理发送Cookie时返回502 - Reverse-proxy to nodejs with Nginx returns a 502 when sending cookies Kubernetes Nginx入口控制器返回502,但仅用于AJAX / XmlHttpRequest请求 - Kubernetes nginx ingress controller returns 502 but only for AJAX/XmlHttpRequest requests laravel websocket 与 nuxt 和 nginx 反向代理返回 502 - laravel websocket with nuxt and nginx reverse proxy returns 502 Nginx 服务器为某些目录返回错误 502(快速应用程序) - The Nginx server returns error 502 for some directories (Express Application)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM