[英]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:
它具有以下组件:
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.