简体   繁体   English

Nginx在proxy_pass之后使用Upgrade头

[英]Nginx consumes Upgrade header after proxy_pass

So I have been banging my head against the wall for the better part of 2 days, please help. 所以我在2天的大部分时间里一直撞到墙上,请帮忙。

I am attempting to establish a Websocket connection using this django-websocket-redis configuration. 我正在尝试使用此django-websocket-redis配置建立Websocket连接。 There are 2 instances of uwsgi running, one for the website and one for the websocket communication. 有两个uwsgi运行实例,一个用于网站,一个用于websocket通信。

I used wireshark heavily to find out what exactly is happening, and apparently nginx is eating the headers "Connection: Upgrade" and "Upgrade: websocket". 我大量使用wireshark来找出究竟发生了什么,显然nginx正在吃标题“Connection:Upgrade”和“Upgrade:websocket”。

here is the critical nginx config part: 这是关键的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;
}

As you can see on those 2 screenshots , tcpdump of internal communication shows that the handshake works nicely. 正如您在这两个屏幕截图中看到的那样,内部通信的tcpdump显示握手工作正常。 but in my browser (second image) the headers are missing. 但在我的浏览器(第二张图片)中,标题丢失了。

Any ideas are greatly appreciated. 任何想法都非常感谢。 I am truly stuck here :( 我真的被困在这里:(

Versions: 版本:

nginx - 1.7.4
uwsgi - 2.0.7

pip freeze: Django==1.7 MySQL-python==1.2.5 django-redis-sessions==0.4.0 django-websocket-redis==0.4.2 gevent==1.0.1 greenlet==0.4.4 redis==2.10.3 six==1.8.0 uWSGI==2.0.7 wsgiref==0.1.2 pip freeze:Django == 1.7 MySQL-python == 1.2.5 django-redis-sessions == 0.4.0 django-websocket-redis == 0.4.2 gevent == 1.0.1 greenlet == 0.4.4 redis == 2.10.3六== 1.8.0 uWSGI == 2.0.7 wsgiref == 0.1.2

I would use gunicorn for deploying a django application, but anyway. 我会使用gunicorn来部署django应用程序,但无论如何。

I remembered that I saw this on the gunicorn docs: 我记得我在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. 如果您希望能够处理流请求/响应或Comet,Long轮询或Web套接字等其他奇特功能,则需要关闭代理缓冲。 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; 要关闭缓冲,您只需要关闭proxy_buffering; to your location block: 到你的位置块:

In your location would be: 在您的位置将是:

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;
}

Link to the guide of gunicorn for deploying in nginx. 链接到gunicorn指南,用于在nginx中部署。 http://docs.gunicorn.org/en/latest/deploy.html?highlight=header http://docs.gunicorn.org/en/latest/deploy.html?highlight=header

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM