Nginx在proxy_pass之后使用Upgrade头

时间:2014-10-03 17:30:42

标签: django nginx websocket uwsgi

所以我在2天的大部分时间里一直撞到墙上,请帮忙。

我正在尝试使用它建立Websocket连接 django-websocket-redis配置。 有两个uwsgi运行实例,一个用于网站,一个用于websocket通信。

我大量使用wireshark来查明究竟发生了什么,显然nginx正在吃标题“Connection:Upgrade”和“Upgrade:websocket”。

这是关键的nginx配置部分:

upstream websocket {
    server 127.0.0.1:9868;
}

location /ws/ {
    proxy_pass_request_headers      on;
    access_log off;
    proxy_http_version 1.1;
    proxy_pass http://websocket;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Upgrade websocket;
}

正如你在那些2 screenshots上看到的那样,内部通信的tcpdump表明握手工作得很好。但是在我的浏览器中(第二张图片)标题丢失了。

非常感谢任何想法。我真的被困在这里:(

版本:

nginx - 1.7.4
uwsgi - 2.0.7

pip冻结:     Django的== 1.7     在MySQL-python的== 1.2.5     Django的Redis的-会议== 0.4.0     Django的WebSocket的,Redis的== 0.4.2     GEVENT == 1.0.1     greenlet == 0.4.4     Redis的== 2.10.3     6 == 1.8.0     uWSGI == 2.0.7     ==的wsgiref 0.1.2

1 个答案:

答案 0 :(得分:1)

我会使用gunicorn来部署django应用程序,但无论如何。

我记得我在gunicorn docs上看到了这个:

  

如果您希望能够处理流请求/响应或其他   你需要的是Comet,Long polling或Web socket等奇特的功能   关闭代理缓冲。当你这样做时,你必须运行一个   异步工作者类。

     

要关闭缓冲,您只需要关闭proxy_buffering;至   你的位置栏:

在您的位置将是:

location /ws/ {
    proxy_pass_request_headers      on;
    access_log off;
    proxy_http_version 1.1;
    proxy_redirect off;
    proxy_buffering off;
    proxy_pass http://websocket;
    proxy_set_header Connection "upgrade";
    proxy_set_header Upgrade websocket;
}

链接到gunicorn用于在nginx中部署的指南。 http://docs.gunicorn.org/en/latest/deploy.html?highlight=header

希望这有帮助