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

2021-04-20 10:58:20 +08:00
 alittlecode

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
2145 次点击
所在节点    Python
26 条回复
Acoffice
2021-04-20 11:12:30 +08:00
nginx 配置 server 块上方增加

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
alittlecode
2021-04-20 11:14:55 +08:00
@Acoffice 不行还是 502
Acoffice
2021-04-20 11:17:07 +08:00
@alittlecode #2 先确定你的 uwsgi 能访问,然后再看你的 nginx 日志
alittlecode
2021-04-20 11:22:14 +08:00
@Acoffice upstream prematurely closed connection while reading response header from upstream, client 这是 nginx 报错, 其他功能是可以访问的 uwsgi 没问题就是 websocket 报错
Acoffice
2021-04-20 11:29:14 +08:00
@alittlecode #4 看起来像是你 django 没有给你返回东西
alittlecode
2021-04-20 11:36:12 +08:00
@Acoffice 我本地测试跑的是没问题的,我用工具连接 websocket 也连接不上没加了个日志进入这个视图就执行,但是也没执行
Acoffice
2021-04-20 11:49:15 +08:00
@alittlecode #6 去掉 proxy_http_version 1.1; 试试吧 不行的话,你就再看看.
alittlecode
2021-04-20 11:53:55 +08:00
@Acoffice 还是不行,我再看看,多谢啊
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
2021-04-20 14:14:25 +08:00
你可以试试把 daemonize = /root/conf/logs/uwsgi_hzmj.log 这个属性去掉看看
alittlecode
2021-04-20 14:19:20 +08:00
@allisone 这个是日志,应该没关系吧,我试了也不行
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
2021-04-20 14:26:45 +08:00
https://blog.csdn.net/by_side_with_sun/article/details/83090506 这篇文章说 uwsgi 不支持 dwebsocket 真假??
alittlecode
2021-04-20 14:34:42 +08:00
@allisone 我设置了 socket-timeout 也不行
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
2021-04-20 15:10:49 +08:00
uwsgi 就是个黑箱 前一阵子受不了 果断弃坑
alittlecode
2021-04-20 15:14:04 +08:00
@Latin 看一下你 gunicorn 配置
alittlecode
2021-04-20 15:41:55 +08:00
刚才试了一下 gunicorn websocket 可以实现
alittlecode
2021-04-20 15:42:25 +08:00
https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/WebSockets.html 但是 uwsgi2.0 之后已经支持 websocket 了 为什么不行
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

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/771852

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX