问题在于websocket连接,由龙卷风/ tornado-sockjs代理nginx到浏览器中的sockjs客户端。没有nginx(在本地主机上),事情就能正常工作。
加载页面时浏览器中的错误消息(初始化sockjs连接时):
WebSocket connection to 'ws://my-domain.com/ws/844/blvz89pq/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
自定义nginx配置的相关部分:
location /ws/ {
proxy_pass http://127.0.0.1:8888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
来自nginx.conf的$ connection_upgrade的值:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
如果Upgrade
标头未设置为'websocket'
(code),看起来龙卷风-sockjs会返回400。实际上,在第46行if
之前打印标题会为我们提供标题Connection=close
的值,而不会显示Upgrade
。
dict(self.request.headers)
的价值
{
"Accept-Language": "en-US,en;q=0.8,ru;q=0.6",
"Accept-Encoding": "gzip, deflate, sdch",
"Sec-Websocket-Key": "*******",
"Connection": "close",
"X-Real-Ip": "172.17.0.1",
"Origin": "http://my-domain.com",
"Sec-Websocket-Extensions": "permessage-deflate; client_max_window_bits",
"X-Forwarded-For": "172.17.0.1",
"Sec-Websocket-Version": "13",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36",
"Host": "my-domain.com",
"X-Forwarded-Proto": "http",
"Cookie": "_ym_uid=1456149826627418608; sessionid=0pn2knqey7raw7w4dst9d7vqvtvsmook; csrftoken=0o1lqSW4XCZvvlvYtN4S7Stt53AyKWf6; __utmx=147844469.bAGHuGeARZy_vppa6db8lg$115132382-10:5.mu9KcCiZSDepsLZ385hDXA$117448007-11:0; __utmxx=147844469.bAGHuGeARZy_vppa6db8lg$115132382-10:1456307221:15552000.mu9KcCiZSDepsLZ385hDXA$117458007-11:1457421562:15552000; _ym_isad=0; _ga=GA1.2.2002408319.1456149825; JSESSIONID=dummy",
"Pragma": "no-cache",
"Cache-Control": "no-cache"
}
但是检查来自浏览器的标题显示,Connection和Upgrade标题都有值,这些值应该对龙卷风有效。
来自浏览器的标题:
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,ru;q=0.6
Cache-Control:no-cache
Connection:Upgrade
Cookie:_ym_uid=1456149826627418608; sessionid=0pn2knqey7raw7w4dst9d7vqvtvsmook; __utmx=147844469.bAGHuGeARZy_vppa6db8lg$115132382-10:5.mu9KcCiZSDepsLZ385hDXA$117448007-11:0; __utmxx=147844469.bAGHuGeARZy_vppa6db8lg$115132382-10:1456307221:15552000.mu9KcCiZSDepsLZ385hDXA$117448007-11:1457421562:15552000; _ym_isad=0; _ga=GA1.2.2002408319.1456149825; csrftoken=0o1lqSW4XCZvvlvYtN4S7Stt53AyKWf6; JSESSIONID=dummy
Host:my-domain.com
Origin:http://my-domain.com
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:/zRJijtAtPjjLNJK49SIDQ==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
我可能的建议是Tornado如何/何时解析标题。
UPD: 使用的软件版本:
1. sockjs-tornado==1.0.2
2. tornado==4.3
3. tornado-redis==2.4.18
答案 0 :(得分:1)
+1到Riliam answer,但我需要加5美分,因为这个问题最近才出现。 因此,要解决此问题,如果您在nginx驱动的docker容器前面有首席 nginx,则需要修改主管 nginx配置。
<强> /etc/nginx/nginx.conf 强>
...
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
...
<强> /etc/nginx/conf.d/virtual-site.conf 强>
8001 - 从泊坞窗容器中露出的端口
location / {
proxy_pass http://127.0.0.1:8001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
答案 1 :(得分:0)
所以问题在于通过在Docker容器之外运行的另一个Nginx代理请求。