I'm using Apache with the mod_proxy
module to reverse proxy my Node.js application through to port 80, so that we can access it as an internal application.
I have a file in sites-enabled
which contains this:
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>
As I said, our application is written in Node.js and we're using socket.io to make use of web-sockets, as our application also contains realtime elements to it. The problem is, mod_proxy
doesn't seem to handle web sockets and we get errors when trying to use them:
WebSocket connection to 'ws://bloot/socket.io/1/websocket/nHtTh6ZwQjSXlmI7UMua' failed: Unexpected response code: 502
How can we fix this issue and keep sockets working, as the only way we can get it working currently is to access the site via ip:port
which we don't want to do.
Also, as a side question, how can I get ErrorDocument
to work properly? Our error files are stored in /var/www/internal/public/error/
but they seem to get put through the proxy too?
Best Answer
In Apache 2.2, web sockets are not supported by reverse proxy but I guess Apache 2.4 have. You have three options to solve this issue.
1) Either you upgrade to 2.4
2) You can use polling (xhr-polling) instead of web sockets. All you need to do is configure transport method in server side Node.js code like this.
This way socket.io will not attempt to figure out the best transport method. It will directly use xhr-polling which is simply Ajax based polling and works in every browser and web server without any issue.
3) If you still want to use web sockets, you can recompile Apache 2.2 with web socket support. Check this article for more detail on that: http://blog.cafarelli.fr/post/2013/04/26/Backporting-Apache-support-for-websockets-reverse-proxy-%28aka-getting-GateOne-to-work-behind-Apache%29