【问题标题】:nginx + python + websocketsnginx + python + websockets
【发布时间】:2013-03-27 11:52:33
【问题描述】:

如何配置 nginx(最新版本,他们说它支持 websockets)以支持 WebSockets。

以及如何使用 python 来运行 websockets 连接。

这就是我想要的:

  • 客户端使用 JavaScript 创建 WebSocket;
  • websocket 服务器脚本在 python 上运行;
  • 所有这一切的后端都是 nginx。

任何人都可以帮助我吗?

【问题讨论】:

    标签: python nginx websocket


    【解决方案1】:

    我快速浏览了与 Nginx 相关的 changeset,看起来您开始处理 websocket 请求所需要做的就是在您的 nginx 配置中设置一个代理。比如:

    upstream pythonserver {
        server localhost:5000;
    }
    
    server {
        // normal server config stuff...
    
        location /some/uri/here {
            // Minimum required settings to proxy websocket connections
            proxy_pass http://pythonserver;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    
            // Other settings for this location
        }
    }
    

    这个小配置 sn-p 会将传入的 websocket 流量代理到您的 Python 应用程序服务器,假设在示例中侦听端口 5000 上的本地连接。

    希望这会有所帮助。

    【讨论】:

    • 谢谢,非常有用。根据/some/uri/herehttp://pythonserver,它在客户端和python服务器脚本中应该如何看待?
    猜你喜欢
    • 2013-05-04
    • 2011-08-09
    • 2013-02-18
    • 1970-01-01
    • 2013-04-06
    • 2021-09-17
    • 1970-01-01
    • 2022-06-29
    • 2012-05-20
    相关资源
    最近更新 更多