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

希望这可以帮助

相关文章

文章浏览阅读3.7k次,点赞2次,收藏5次。Nginx学习笔记一、N...
文章浏览阅读1.7w次,点赞14次,收藏61次。我们在使用容器的...
文章浏览阅读1.4k次。当用户在访问网站的过程中遇到404错误时...
文章浏览阅读2.7k次。docker 和 docker-compose 部署 nginx+...
文章浏览阅读1.3k次。5:再次启动nginx,可以正常启动,可以...
文章浏览阅读3.1w次,点赞105次,收藏182次。高性能:Nginx ...