關於dwebsocket部署 uwsgi、nginx的那些報錯

所有操作均在ubuntu下測試進行!!!

  • 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。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章