nginx代理websockets,一定是缺less的东西

我有一个基本的聊天应用程序,使用express和socket.io在node.js中编写; 直接连接到端口3000上的节点时,它工作正常

但是,当我尝试使用nginx v1.4.2作为代理时,这并不起作用。

我开始使用连接图

map $http_upgrade $connection_upgrade { default upgrade; '' close; } 

然后添加位置

 location /socket.io/ { proxy_pass http://node; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Request-Id $txid; proxy_set_header X-Session-Id $uid_set+$uid_got; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_buffering off; proxy_read_timeout 86400; keepalive_timeout 90; proxy_cache off; access_log /var/log/nginx/webservice.access.log; error_log /var/log/nginx/webservice.error.log; } location /web-service/ { proxy_pass http://node; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Request-Id $txid; proxy_set_header X-Session-Id $uid_set+$uid_got; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_buffering off; proxy_read_timeout 86400; keepalive_timeout 90; access_log /var/log/nginx/webservice.access.log; error_log /var/log/nginx/webservice.error.log; rewrite /web-service/(.*) /$1 break; proxy_cache off; } 

这些都是使用所有的技巧,使我能find它的工作。 错误日志不显示任何错误。 (除了当我停止节点来testing错误日志logging工作)

当通过nginx的时候,我在开发工具中看到一个websocket连接,状态为101; 但resuects下的框架标签是空的。 唯一不同的地方,我可以看到在响应头是一个案例的差异 – “升级”与“升级” – 通过nginx:

 Connection:upgrade Date:Fri, 08 Nov 2013 11:49:25 GMT Sec-WebSocket-Accept:LGB+iEBb8Ql9zYfqNfuuXzdzjgg= Server:nginx/1.4.2 Upgrade:websocket 

直接从节点

 Connection:Upgrade Sec-WebSocket-Accept:8nwPpvg+4wKMOyQBEvxWXutd8YY= Upgrade:websocket 

从节点输出(当通过nginx使用时)

 debug - served static content /socket.io.js debug - client authorized info - handshake authorized iaej2VQlsbLFIhachyb1 debug - setting request GET /socket.io/1/websocket/iaej2VQlsbLFIhachyb1 debug - set heartbeat interval for client iaej2VQlsbLFIhachyb1 debug - client authorized for debug - websocket writing 1:: debug - websocket writing 5:::{"name":"message","args":[{"message":"welcome to the chat"}]} debug - clearing poll timeout debug - jsonppolling writing io.j[0]("8::"); debug - set close timeout for client 7My3F4CuvZC0I4Olhybz debug - jsonppolling closed due to exceeded duration debug - clearing poll timeout debug - jsonppolling writing io.j[0]("8::"); debug - set close timeout for client AkCYl0nWNZAHeyUihyb0 debug - jsonppolling closed due to exceeded duration debug - setting request GET /socket.io/1/xhr-polling/iaej2VQlsbLFIhachyb1?t=1383911206158 debug - setting poll timeout debug - discarding transport debug - cleared heartbeat interval for client iaej2VQlsbLFIhachyb1 debug - setting request GET /socket.io/1/jsonp-polling/iaej2VQlsbLFIhachyb1?t=1383911216160&i=0 debug - setting poll timeout debug - discarding transport debug - clearing poll timeout debug - clearing poll timeout debug - jsonppolling writing io.j[0]("8::"); debug - set close timeout for client iaej2VQlsbLFIhachyb1 debug - jsonppolling closed due to exceeded duration debug - setting request GET /socket.io/1/jsonp-polling/iaej2VQlsbLFIhachyb1?t=1383911236429&i=0 debug - setting poll timeout debug - discarding transport debug - cleared close timeout for client iaej2VQlsbLFIhachyb1 

当直接到节点时,客户端不会开始轮询。

正常的http stuff节点输出对于nginx工作正常。

显然我没有看到的东西,但我卡住了,谢谢:)

你有没有尝试一个最小的configuration,如:

 location /socket.io/ { proxy_pass http://node; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; } 

另请注意,从$ http_host到$ host的更改在某些情况下可能会有所不同: https ://stackoverflow.com/questions/15414810/whats-the-difference-of-host-and-http-host-in- nginx的

当我介绍用于代理Web Socket的nginx时,我遇到了同样的问题。 问题是我强制在nodejs连接立即Web Socket。 删除这个力量,它首先在轮询协商,然后自动升级:

在客户端,我评论了交通工具:

 //transports:['websocket', 'polling'] this was not permiting my connection with nginx io(this.adress, {query: 'auth_cookie='+this.auth_cookie}); 

另外我删除了服务器端的传输configuration。

为了完整性,这是我的nginxconfiguration:

 server { listen 80; server_name demo2.example.com; location /socket.io { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_pass http://demo2.example.com:3012/socket.io; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; } location / { proxy_pass http://demo2.example.com:81; } } 

希望这可以帮助