2016-10-24 65 views
-1

我正在使用Raspberry Pi構建的攝像頭。它具有以下組件:如果服務未準備就緒,Nginx會返回502

  • Node.js的3000端口
  • 的FFmpeg/ffserver的端口8090
  • 的Nginx在前面的代理這些服務一起通過端口80/443(HTTP/HTTPS)

我遇到的問題是,如果在Nginx啓動時FFserver在端口8090上沒有完全準備好,即使流正在運行,它也將持續返回502的stream.mjpeg。就好像Nginx確定主機不存在,然後再也不會再嘗試。一旦我重新啓動/重新加載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; 
    } 
} 
+0

如果確實有一個應用程序在8090上運行,那麼不應該發生這種情況。您確認您的應用程序正在運行嗎?關於防火牆問題呢?有什麼東西在8090上聽? (例如'netstat -atun | grep 8090'可能會給出一些指示)。作爲一個方面說明:您不需要將'/ stream.mjpeg'附加到'proxy_pass'。 NGINX爲你做這件事,因爲它落在那個位置塊。 –

+0

我可以通過直接轉到該端口來確認服務正在運行。他們確實需要分開的地點,因爲他們在不同的港口。不過,我相信我解決了它,所以請參閱下面的答案。 – jocull

回答

1

/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; 
    } 
+0

很好解決。從未見過這個問題,謝謝! –