关于dwebsocket部署 uwsgi、nginx的那些报错 坑我一天

uwsgi

 

#dwebsocket作者在github详细说明了配置要求:https://github.com/duanhongyi/dwebsocket

#If you want to use the uwsgi backend, add WEBSOCKET_FACTORY_CLASS in the settings.py file:
WEBSOCKET_FACTORY_CLASS = 'dwebsocket.backends.uwsgi.factory.uWsgiWebSocketFactory'
#Run uwsgi:
uwsgi --http :8080 --http-websockets --processes 1 --wsgi-file wsgi.py--async 30 --ugreen --http-timeout 300

 

以下,均是我测试过程中碰到的报错:

WebSocket connection to ‘ws://*****’ failed: Error during WebSocket handshake: Unexpected response code: 400
#Solution
#在django的setting.py添加:
WEBSOCKET_FACTORY_CLASS = 'dwebsocket.backends.uwsgi.factory.uWsgiWebSocketFactory'

正常连接websocket(通过request.is_websocket()判断为True),Status Code为101,但django收不到消息(request.websocket没有数据)。

#Solution
我遇到这种情况大部分是uwsgi配置有问题,以下是我遇上这个问题的解决办法
1.在配置文件uwsgi.ini添加
http-websockets = True
2.错误配置了enable-threads
在配置文件uwsgi.ini删除或注释
#enable-threads = True
就可以收到消息了

no app loaded. going in full dynamic mode

 

#Solution
sudo apt-get remove uwsgi
sudo apt-get update
#创建并进入虚拟环境
sudo pip install uwsgi

uwsgi ASYNC call without async mode !!!

#Solution
#threads = 3 #删掉!
async = 30
ugreen = True

nginx
需求是配置成https访问websocket,均是我测试过程中碰到的报错:

WebSocket connection to ‘*****’ failed: Error in connection
establishment: net::ERR_SSL_PROTOCOL_ERROR

 

复制代码
#Solution
#websocket 连接的域名不支持wss协议,那就用nginx配置个wss。
nginx添加如下(最简配置):
location /wss {
                   proxy_http_version 1.1;
                   proxy_set_header Upgrade $http_upgrade;
                   proxy_set_header Connection "upgrade";
                   proxy_pass http://***; #代理到你的服务地址
   }
复制代码

Mixed Content: The page at ‘’ was loaded over HTTPS, but
attempted to connect to the insecure WebSocket endpoint 'ws://’.
This request has been blocked; this endpoint must be available over
WSS.
Uncaught DOMException: Failed to construct ‘WebSocket’: An
insecure WebSocket connection may not be initiated from a page loaded
over HTTPS.

#Solution
报错写的很清楚,当前页面配置为https,但客户端试图连接到不安全的websocket端点'ws://',请求被阻止,所以客户端请求改成'wss://'就好了
wss协议实际是websocket+SSL,就是在websocket协议上加入SSL层,类似https:http+SSL。

 

posted @   今天的诸葛亮  阅读(1563)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示