V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
kayseen
V2EX  ›  Python

flask-socketio 部署到服务器的问题

  •  
  •   kayseen · 2019-12-13 17:51:47 +08:00 · 4668 次点击
    这是一个创建于 1586 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用的服务器是centos7,部署使用的是 nginx + python3.7 + super + gunicorn + eventlet,

    部署的代码是flask-socketio的官方的 example 源码,链接为https://github.com/miguelgrinberg/Flask-SocketIO/tree/master/example,

    这份代码在本地跑起来是没有问题的,但是部署到服务器之后,浏览器的控制台一直报错,页面不能正常通信,并且一直尝试连接,每一秒钟都会更新一次 sid,浏览器的控制台报错如下:

    index.js:83 POST http://192.168.1.110:9001/socket.io/?EIO=3&transport=polling&t=Mx-f5Fy&sid=85cb69f8fd484f5287b865a1cfe 400 (BAD REQUEST)
    
    WebSocket connection to 'ws://192.168.1.110:9001/socket.io/?EIO=3&transport=websocket&sid=85cb69f8fd484f5287b865a1cfe' failed: WebSocket is closed before the connection is established.
    

    我的 nginx 配置如下:

    说明一下,proxy_pass 到 20001 是因为我的 super 托管到这个端口的,所以 20001 的 api 这个时候服务还是正常的

    server {
        listen  9001;
        server_name _;
        access_log /var/log/nginx/access-sio.log main;
        error_log /var/log/nginx/error-sio.log;
        error_page 404              /404.html;
        error_page 400 502 503 504  /50x.html;
    
        location = / {
            root /data/www/sio/;
            index index.html;
        }
    
        location / {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            include proxy_params;
            proxy_pass http://127.0.0.1:20001/;
            proxy_redirect off;
        }
    
        location /socket.io {
            include proxy_params;
            proxy_http_version 1.1;
            proxy_buffering off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_pass http://127.0.0.1:20001/socket.io;
     }
    }
    
    

    麻烦有了解的帮忙解答一下,这个问题我在网上找了,都是说在 nginx 中添加配置,但是配置我已经加进去了,

    现在实在不知道是啥问题了...

    9 条回复    2019-12-18 18:06:59 +08:00
    Latin
        1
    Latin  
       2019-12-13 18:25:45 +08:00
    加配置重启 nginx 了吗?
    sidegem
        2
    sidegem  
       2019-12-13 19:15:55 +08:00
    location /ws { # For websocket support
    proxy_pass http://10.1.1.13:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade websocket;
    proxy_set_header Connection upgrade;
    proxy_read_timeout 86400;
    }
    少个 proxy_set_header 配置?
    superrichman
        3
    superrichman  
       2019-12-13 21:10:56 +08:00
    eventlet 换 gevent 试试
    kayseen
        4
    kayseen  
    OP
       2019-12-14 13:01:37 +08:00 via Android
    @Latin 嗯呢,重启了的,还是不行
    kayseen
        5
    kayseen  
    OP
       2019-12-14 14:11:50 +08:00
    @sidegem
    试过了,你这个配置也不通.. 我上面的配置是官方提供的....
    chuxiaonan
        6
    chuxiaonan  
       2019-12-14 15:02:03 +08:00 via iPhone
    当时似乎也遇到过 好像是版本的问题导致的
    客户端和服务端全部降版本 然后锁住 重试一下试试
    kayseen
        7
    kayseen  
    OP
       2019-12-16 08:54:23 +08:00
    @chuxiaonan
    你好,麻烦问一下,你说的是 nginx 的版本还是 socketio 的版本还是前端使用的版本呢?
    前端和 socketio 版本的话是官方提供的案例里的,
    十分感谢帮忙解答
    geebos
        8
    geebos  
       2019-12-17 18:37:43 +08:00
    把前面两个 location / 移到 location /socket.io 后面试试,可能是匹配路径的时候把连接转发到前面两个路径去了
    kayseen
        9
    kayseen  
    OP
       2019-12-18 18:06:59 +08:00
    @geebos
    感谢回复哈~
    这个方法试过了也没用===
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3518 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:37 · PVG 19:37 · LAX 04:37 · JFK 07:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.