将nginx部署到log.io前端后发现功能虽然正常,但控制台有错误记录
WebSocket connection to 'ws://logs.renjie.me/socket.io/1/websocket/QqAw_JLwsOqC_w1kG6mr' failed: Error during WebSocket handshake: Unexpected response code: 502
接着就是刷屏式的如下请求了
XHR finished loading: GET "http://logs.renjie.me/socket.io/1/xhr-polling/QqAw_JLwsOqC_w1kG6mr?t=1450325139049".
猜测肯定遇到降级处理了,找到了其依赖的socket.io源码确认果然如此
module.exports = {
websocket: require('./websocket')
, flashsocket: require('./flashsocket')
, htmlfile: require('./htmlfile')
, 'xhr-polling': require('./xhr-polling')
, 'jsonp-polling': require('./jsonp-polling')
};
Remote Address:120.25.163.48:80
Request URL:http://logs.renjie.me/socket.io/1/?t=1450326393264
Request Method:GET
Status Code:200 OK
==================》
Connection:keep-alive
Content-Type:text/plain
Date:Thu, 17 Dec 2015 04:26:33 GMT
Server:nginx/1.6.3
Transfer-Encoding:chunked
poo25heDWcamJuUXG6mt:60:60:websocket,htmlfile,xhr-polling,jsonp-polling
那问题基本归根与nginx对于websocket这种新协议的代理问题,简单优化配置如下
upstream logs {
server 127.0.0.1:28778;
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name logs.renjie.me;
location / {
proxy_pass http://logs;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
可见不用新开端口服务,直接在现有的http端口通道上进行改造即可
同时附上websocket握手协议的相关记录以方便日后进行深入研究
Request URL:ws://logs.renjie.me/socket.io/1/websocket/poo25heDWcamJuUXG6mt
Request Method:GET
Status Code:101 Switching Protocols
Request Headers
Connection:Upgrade
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:liT05Oj1ZyHrhtj8E7YBAw==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
Response Headers
Connection:upgrade
Sec-WebSocket-Accept:Xufj5Cntpx6HMfTp2TVRyfp9SYw=
Server:nginx/1.6.3
Upgrade:websocket
|