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
![]() | 1 Acoffice 2021-04-20 11:12:30 +08:00 nginx 配置 server 块上方增加 map $http_upgrade $connection_upgrade { default upgrade; '' close; } |
2 alittlecode OP @Acoffice 不行还是 502 |
![]() | 3 Acoffice 2021-04-20 11:17:07 +08:00 @alittlecode #2 先确定你的 uwsgi 能访问,然后再看你的 nginx 日志 |
4 alittlecode OP @Acoffice upstream prematurely closed connection while reading response header from upstream, client 这是 nginx 报错, 其他功能是可以访问的 uwsgi 没问题就是 websocket 报错 |
![]() | 5 Acoffice 2021-04-20 11:29:14 +08:00 @alittlecode #4 看起来像是你 django 没有给你返回东西 |
6 alittlecode OP @Acoffice 我本地测试跑的是没问题的,我用工具连接 websocket 也连接不上没加了个日志进入这个视图就执行,但是也没执行 |
![]() | 7 Acoffice 2021-04-20 11:49:15 +08:00 @alittlecode #6 去掉 proxy_http_version 1.1; 试试吧 不行的话,你就再看看. |
8 alittlecode OP @Acoffice 还是不行,我再看看,多谢啊 |
![]() | 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 |
![]() | 10 allisone 2021-04-20 14:14:25 +08:00 你可以试试把 daemOnize= /root/conf/logs/uwsgi_hzmj.log 这个属性去掉看看 |
11 alittlecode OP @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 的守护日志就可以了。。 |
13 alittlecode OP https://blog.csdn.net/by_side_with_sun/article/details/83090506 这篇文章说 uwsgi 不支持 dwebsocket 真假?? |
14 alittlecode OP @allisone 我设置了 socket-timeout 也不行 |
![]() | 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 大法好(狗头 |
16 echowuhao 2021-04-20 15:10:49 +08:00 via Android uwsgi 就是个黑箱 前一阵子受不了 果断弃坑 |
17 alittlecode OP @Latin 看一下你 gunicorn 配置 |
18 alittlecode OP 刚才试了一下 gunicorn websocket 可以实现 |
19 alittlecode OP https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/WebSockets.html 但是 uwsgi2.0 之后已经支持 websocket 了 为什么不行 |
![]() | 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 |
21 alittlecode OP @Latin 我添加完 http-websockets 这个参数还是不行,现在用 gunicorn 可以实现了,但是 30 秒就自动断开了,唉,处处是坑 |
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 守护进程之后,目前来看没得什么问题,实践中也没有发现什么问题,但是对性能有没有什么消耗,就不得而知了 |
23 Aprilming 2021-04-20 18:12:42 +08:00 @alittlecode #19 我也试过 uwsgi 的 websocket, 奈何当时实在是没有成功,如果你成功了,能否分享一下经验,非常感谢。 |
24 alittlecode OP @Aprilming 使用 daphne 再启动一个 asgi 服务是吧,我看到这个方法了,但是觉得太麻烦了,直接使用 gunicorn 可以了 |
25 alittlecode OP @Aprilming 但是现在还有个问题是 30 秒自动断开了,我看有说 nginx 设置 proxy_read_timeout time 参数的,不过貌似这影响性能,没去试,暂时还没解决 |
26 Aprilming 2021-04-20 18:16:15 +08:00 @alittlecode #24 gunicorn 问题不大,因为之前考虑到 uwsgi 使用套接字性能比端口转发要好, 所以最后还是使用了 uwsgi+daphne 。 |