微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

django – Nginx在proxy_pass之后使用升级头

所以我在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

最佳答案
我会使用gunicorn来部署django应用程序,但无论如何.

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

If you want to be able to handle streaming request/responses or other
fancy features like Comet,Long polling,or Web sockets,you need to
turn off the proxy buffering. When you do this you must run with one
of the async worker classes.

To turn off buffering,you only need to add proxy_buffering off; to
your location block:

在您的位置将是:

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

希望这可以帮助

原文地址:https://www.jb51.cc/nginx/434800.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐