[英]socket.io trouble in nginx
socket.io v。1.0
nginx v 1.4.6
nodejs v 0.10.32
ubuntu v 14.04
小滴IP(例如):123.45.678.90
nodejs端口侦听:3001
域:mydomain.com
Everythigs既可以在WebStorm中的localhost上运行,也可以在我的Droplet地址上正常运行,例如:123.45.678.90:3001。 当我尝试通过以下配置绑定域和Nginx时,问题就开始了:
server { listen 80; server_name mydomain.com; location / { proxy_pass http://localhost:3001; proxy_http_version 1.1; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
当我尝试从浏览器连接mydomain.com时,有时工作正常,但有时我看到该消息:
{ host: 'mydomain.com', connection: 'close', 'accept-encoding': 'gzip, deflate', 'user-agent': 'WebIndex', accept: 'text/html' } /var/www/bm/socket/index.js:29 var sid = socket.request.headers.cookie.split('express.sid=s%3A')[1].split ^ TypeError: Cannot call method 'split' of undefined
这是我的socket / index.js,其中包括io.use:
io.use(function(socket, next){ async.waterfall([ function(callback){ console.log(socket.request.headers); var sid = socket.request.headers.cookie.split('express.sid=s%3A')[1].split('.')[0]; console.log(sid); sessionStore.load(sid, function(err, session){ if (arguments.length == 0) { return callback(null, null); } else { return callback(null, session) } }); }, function(session, callback){ socket.handshake.session = session; callback(null); } ], function(){ if (socket.handshake.session) { return next(); } else { console.log('error, be careful'); return next(new Error('not authorized')); } }); });
发生了什么事? 我试图捕获“ WebIndex”并更改io.set的一部分:
console.log(socket.request.headers); if (socket.request.headers['user-agent'] == 'WebIndex') { return callback(null, null); } var sid = socket.request.headers.cookie.split('express.sid=s%3A')[1].split('.')[0]; console.log(sid);
结果,我在地址mydomain.com上的浏览器中找到了它:
WebSocket connection to 'ws://mydomain.com/socket.io/?EIO=3&transport=websocket&sid=QzYL8Ou8QN556_WEAAAC' failed: Error during WebSocket handshake: Unexpected response code: 400
几秒钟socket.io正常工作,但随后出现此400错误消息。 有任何想法吗?
我通过完全重新安装nginx解决了该问题:
// Delete nginx and linked packages:
sudo apt-get remove nginx*
// Delete nginx working directories and logs:
sudo rm -rf /etc/nginx/ /usr/sbin/nginx /usr/share/man/man1/nginx.1.gz
// Delete all remaining nginx files from apt base:
sudo apt-get --purge autoremove nginx && sudo dpkg --purge nginx
// Reinstall nginx with pure config files
sudo apt-get -o DPkg::options::=--force-confmiss --reinstall install nginx
而且有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.