我正在使用Apache与mod_proxy模块反向代理我的Node.js应用程序通过端口80,以便我们可以访问它作为一个内部的应用程序。
我有一个sites-enabled的文件,其中包含:
VirtualHost *:80> DocumentRoot /var/www/internal/ ServerName internal ServerAlias internal <Directory /var/www/internal/public/> Options All AllowOverride All Order allow,deny Allow from all </Directory> ProxyRequests off <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://localhost:8080/ retry=0 ProxyPassReverse / http://localhost:8080/ ProxyPreserveHost on ProxyTimeout 1200 LogLevel debug AllowEncodedSlashes on </VirtualHost>
正如我所说的,我们的应用程序是用Node.js编写的,我们使用socket.io来使用web-sockets,因为我们的应用程序还包含实时元素。 问题是, mod_proxy似乎不处理networking套接字,我们得到错误时,试图使用它们:
WebSocket connection to 'ws://bloot/socket.io/1/websocket/nHtTh6ZwQjSXlmI7UMua' failed: Unexpected response code: 502
我们怎样才能解决这个问题,并保持套接字工作,因为我们目前能够正常工作的唯一方法就是通过ip:port来访问我们不想做的事情。
另外,作为一个侧面的问题,我怎样才能得到ErrorDocument正常工作? 我们的错误文件存储在/var/www/internal/public/error/但是它们似乎也通过代理了?
在Apache 2.2中,反向代理不支持Web套接字,但我猜Apache 2.4已经支持。 你有三个select来解决这个问题。
1)要么升级到2.4
2)您可以使用轮询(xhr轮询),而不是networking套接字。 所有你需要做的就是像这样在服务器端Node.js代码中configuration传输方法。
var io = require('socket.io').listen(PORT); io.configure(function () { io.set("transports", ["xhr-polling"]); });
这样socket.io将不会试图找出最佳的传输方法。 它将直接使用xhr-polling,它是简单的基于Ajax的轮询,可以在任何浏览器和Web服务器中正常工作。
3)如果你仍然想使用networking套接字,你可以重新编译Apache 2.2的networking套接字支持。 检查这篇文章的更多细节: http : //blog.cafarelli.fr/post/2013/04/26/Backporting-Apache-support-for-websockets-reverse-proxy-%28aka-getting-GateOne-to-工作背后的Apache 29%