• 关于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。
  • 相关阅读:
    用好idea这几款插件,可以帮你少写30%的代码
    面试官:一个TCP连接可以发多少个HTTP请求?
    php5.4、5.5、5.6高版本中htmlspecialchars兼容性处理
    IIS8的SNI功能实现同一服务器多HTTPS站点
    appache 在windows 中无法启动的测试
    lumen 支持多文件上传及php 原生多文件上传
    laravel windows安装
    jQuery 为动态添加的元素绑定事件
    thinkphp 5.0 在appache下隐藏index.php入口代码
    thinkphp5.0 分页中伪静态的处理
  • 原文地址:https://www.cnblogs.com/gaosai/p/13160485.html
Copyright © 2020-2023  润新知