2017-09-10 10 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