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
alittlecode
V2EX  ›  Python

django uwsgi nginx websocket 搭建后台管理,本地没问题,上传服务器 websocket 报 502 错误,其他接口能正常访问

  •  
  •   alittlecode · 2021-04-20 10:58:20 +08:00 · 2098 次点击
    这是一个创建于 1074 天前的主题,其中的信息可能已经有所发展或是发生改变。

    nginx 配置: server { listen 80; server_name 服务器地址; charset utf-8; client_max_body_size 75M;

    location / {
        root /root/dist;
        index index.html;
        try_files $uri $uri/ /index.html;
       
    }
    location /api/ {
        uwsgi_pass 127.0.0.1:8001;
        include /etc/nginx/uwsgi_params;  
    }
    location /ws/ {
        proxy_pass http://127.0.0.1:8001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       
    }
    

    } nginx 配置:

    [uwsgi]
    chdir = /root/....
    home = /root/....
    module = ....
    
    master = True
    processes = 2
    harajiri = 60
    max-requests = 5000
    
    socket = 127.0.0.1:8001
    uid = root
    gid = root
    pidfile = /root/master.pid
    daemonize = /root/conf/logs/uwsgi_hzmj.log
    vacuum = True
    log-maxsize = 102400 
    buffer-size = 65536
    

    websocket 报错:

    WebSocket connection to 'ws://服务器地址 /ws/index/data/' failed: Error during WebSocket 		handshake: Unexpected response code: 502
    
    26 条回复    2021-04-20 18:16:15 +08:00
    Acoffice
        1
    Acoffice  
       2021-04-20 11:12:30 +08:00
    nginx 配置 server 块上方增加

    map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
    }
    alittlecode
        2
    alittlecode  
    OP
       2021-04-20 11:14:55 +08:00
    @Acoffice 不行还是 502
    Acoffice
        3
    Acoffice  
       2021-04-20 11:17:07 +08:00
    @alittlecode #2 先确定你的 uwsgi 能访问,然后再看你的 nginx 日志
    alittlecode
        4
    alittlecode  
    OP
       2021-04-20 11:22:14 +08:00
    @Acoffice upstream prematurely closed connection while reading response header from upstream, client 这是 nginx 报错, 其他功能是可以访问的 uwsgi 没问题就是 websocket 报错
    Acoffice
        5
    Acoffice  
       2021-04-20 11:29:14 +08:00
    @alittlecode #4 看起来像是你 django 没有给你返回东西
    alittlecode
        6
    alittlecode  
    OP
       2021-04-20 11:36:12 +08:00
    @Acoffice 我本地测试跑的是没问题的,我用工具连接 websocket 也连接不上没加了个日志进入这个视图就执行,但是也没执行
    Acoffice
        7
    Acoffice  
       2021-04-20 11:49:15 +08:00
    @alittlecode #6 去掉 proxy_http_version 1.1; 试试吧 不行的话,你就再看看.
    alittlecode
        8
    alittlecode  
    OP
       2021-04-20 11:53:55 +08:00
    @Acoffice 还是不行,我再看看,多谢啊
    BillowSky
        9
    BillowSky  
       2021-04-20 13:40:21 +08:00
    socket = 127.0.0.1:8001

    我记得这个协议有几个: http 、http-socket ,你切换看看。


    https://www.cnblogs.com/shanchuan/p/12830443.html
    allisone
        10
    allisone  
       2021-04-20 14:14:25 +08:00
    你可以试试把 daemonize = /root/conf/logs/uwsgi_hzmj.log 这个属性去掉看看
    alittlecode
        11
    alittlecode  
    OP
       2021-04-20 14:19:20 +08:00
    @allisone 这个是日志,应该没关系吧,我试了也不行
    allisone
        12
    allisone  
       2021-04-20 14:23:19 +08:00
    @alittlecode 我以前跑我的项目的时候报的错就是和你的一样 upstream prematurely closed connection while reading response header from upstream 我是用 supervisord 管理 uwsig 进程坑一
    如果通过 supervisor 来管理 uwsgi,那么需要注意选项 daemonize 和 log-maxsize 就不要再配置了,不然就会报错"FATAL FAIL"啥的错误。
    坑二
    如果完成了 nginx+uwsgi+django 的配置,但是有时候访问有的页面(注意是有的页面)会提示 502 bad gateway 查看 nginx 的日志是这样的一行:Upstream prematurely closed connection while reading upstream...
    出现这种情况可能是 django 处理请求时间很长,导致请求还没发出去,就被干掉了,这个时候可以调整 harakiri 或者 socket-timeout 参数的值,我自己现在的博客项目之前遇到就是这样就解决的。
    坑三
    如果没有设置 log-maxsize,则会出现 supervisor 管理当前项目的日志只会输出到 err.log 里面
    后续如果踩到新坑会再添加到该页的,当时我的 uwsi 进程开了守护日志,就不行,看了 nginx 日志和你的一样,我直接关闭 uwsgi 的守护日志就可以了。。
    alittlecode
        13
    alittlecode  
    OP
       2021-04-20 14:26:45 +08:00
    https://blog.csdn.net/by_side_with_sun/article/details/83090506 这篇文章说 uwsgi 不支持 dwebsocket 真假??
    alittlecode
        14
    alittlecode  
    OP
       2021-04-20 14:34:42 +08:00
    @allisone 我设置了 socket-timeout 也不行
    Latin
        15
    Latin  
       2021-04-20 15:09:09 +08:00
    map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
    }
    server {
    listen 80;
    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:5000/socket.io;
    }

    }
    啥年代了 uwsgi 劝退 gunicorn 大法好(狗头
    echowuhao
        16
    echowuhao  
       2021-04-20 15:10:49 +08:00 via Android
    uwsgi 就是个黑箱 前一阵子受不了 果断弃坑
    alittlecode
        17
    alittlecode  
    OP
       2021-04-20 15:14:04 +08:00
    @Latin 看一下你 gunicorn 配置
    alittlecode
        18
    alittlecode  
    OP
       2021-04-20 15:41:55 +08:00
    刚才试了一下 gunicorn websocket 可以实现
    alittlecode
        19
    alittlecode  
    OP
       2021-04-20 15:42:25 +08:00
    https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/WebSockets.html 但是 uwsgi2.0 之后已经支持 websocket 了 为什么不行
    Latin
        20
    Latin  
       2021-04-20 16:27:47 +08:00
    @alittlecode
    uwsgi 官方支持的是 uwsgi websocket 不是第三方 websocket 库
    而且用 websocket 就别用 sock 套接字 直接 http 服务
    文档原文:
    不幸的是,并非所有的 HTTP web 服务器 /代理都与 websockets 工作得很好。

    uWSGI HTTP/HTTPS/SPDY 路由器完美支持它们。只需记得添加 --http-websockets 选项。

    uwsgi --http :8080 --http-websockets --wsgi-file myapp.py
    alittlecode
        21
    alittlecode  
    OP
       2021-04-20 16:44:41 +08:00
    @Latin 我添加完 http-websockets 这个参数还是不行,现在用 gunicorn 可以实现了,但是 30 秒就自动断开了,唉,处处是坑
    Aprilming
        22
    Aprilming  
       2021-04-20 18:10:01 +08:00
    哈哈,我也遇到过,我用的 daphne 解决的 , /ws/和你一样用 nginx 映射到 127.0.0.1:8088, 然后起一个 daphne:daphne xxx(项目).asgi:application -b 127.0.0.1 -p 8088 。用 supervisord 守护进程之后,目前来看没得什么问题,实践中也没有发现什么问题,但是对性能有没有什么消耗,就不得而知了
    Aprilming
        23
    Aprilming  
       2021-04-20 18:12:42 +08:00
    @alittlecode #19 我也试过 uwsgi 的 websocket, 奈何当时实在是没有成功,如果你成功了,能否分享一下经验,非常感谢。
    alittlecode
        24
    alittlecode  
    OP
       2021-04-20 18:13:38 +08:00
    @Aprilming 使用 daphne 再启动一个 asgi 服务是吧,我看到这个方法了,但是觉得太麻烦了,直接使用 gunicorn 可以了
    alittlecode
        25
    alittlecode  
    OP
       2021-04-20 18:15:52 +08:00
    @Aprilming 但是现在还有个问题是 30 秒自动断开了,我看有说 nginx 设置 proxy_read_timeout time 参数的,不过貌似这影响性能,没去试,暂时还没解决
    Aprilming
        26
    Aprilming  
       2021-04-20 18:16:15 +08:00
    @alittlecode #24 gunicorn 问题不大,因为之前考虑到 uwsgi 使用套接字性能比端口转发要好, 所以最后还是使用了 uwsgi+daphne 。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2870 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:28 · PVG 19:28 · LAX 04:28 · JFK 07:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.