2017-09-10 29 views
0

我有一個痛苦的問題。我使用Flask-SocketIO將後臺進程的一些狀態更新到網頁上。 對於我的例子,我的應用程序被放在IP 170.8.8.8監控端口5000的機器A中,並且我把Nginx放在IP 170.8.8.9的機器B中,同時監控5000端口。所以我想在B中訪問IP:5000,到IP:在答:5000以下是在機器B我nginx的配置:網頁只能通過刷新網頁獲得後臺進程的最新狀態

upstream cuitccol.com{ #the name of server cluster 
     server 170.8.8.8:5000 max_fails=5 fail_timeout=50s; #for the first web server 
     } 

    server { 
     listen  5000; 
     server_name localhost; 

     #charset koi8-r; 

     #access_log logs/host.access.log main; 

     location/{ 
      proxy_pass http://cuitccol.com; 
      proxy_http_version 1.1; 
      proxy_set_header Upgrade $http_upgrade; 
      proxy_set_header Connection "upgrade"; 
      proxy_set_header Host $host; 
     } 

如果我直接在訪問網絡,我的瀏覽器的網頁能夠不斷更新後臺進程的狀態。但是如果我訪問放置nginx的B,則網頁不能不斷更新狀態,並且必須刷新網頁以獲取下一個狀態。和我的瀏覽器的控制檯有一個錯誤,如下所示:

WebSocket connection to 'ws://170.8.8.9:5000/socket.io/?EIO=3&transport=websocket&sid=4a2ec29f7f834a0bb289b21f03c3e47c' failed: Error during WebSocket handshake: Unexpected response code: 200 

我不知道哪裏出了問題。 nginx或者flask-socketio。你能提供一些建議嗎? 非常感謝你〜

+0

請參閱Flask-SocketIO的nginx配置文檔:https://flask-socketio.readthedocs.io/en/latest/#using-nginx-as -a-websocket-reverse-proxy – Miguel

+0

好的,你指定的文檔也可以解決我的問題。非常感謝你 – AndrewGong

回答

0

你的配置更改下面

location/{ 
     proxy_pass http://cuitccol.com; 
     proxy_http_version 1.1; 
    } 

    location /socket.io { 
     proxy_pass http://cuitccol.com/socket.io; 
     proxy_http_version 1.1; 
     proxy_buffering off; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection "upgrade"; 
    } 

,看看它是否有幫助。升級頭文件應該用於Socket,而不用於其他端點

+0

你是如此的酷,你的解決方案完全解決了我的問題。非常感謝你~~ – AndrewGong