Previously, I've been using Apache for HTTP serving combined with some python listening on a high-numbered socket for the websocket stuff, but that obviously won't work here.
I can always move the websocket stuff to a separate server, but I'd like to avoid paying for a second VPS (and have to talk to the database over the network instead of locally). Is there a good way to do this (nodejs, nginx, ..?), or is it not worth the headache?
YES, by using node.js. Express or connect for the HTTP file serving and socket.io for the WebSocket stuff.
Example:
var express = require("express"); var app = express.createServer(); app.get('/', function(req, res){ res.redirect("/index.html"); }); app.configure(function(){ app.use(express.static(__dirname + '/public')); }); app.listen(80); var io = require('socket.io'); var socket = io.listen(app); socket.on('connection', function(client){ client.on('message', function(){...}); })
Another possibility is to use mod_proxy in apache to redirect the requests to a websocket server.
Of course you can do this.
Firstly you have to check your Apache version. You should have 2.4+ version. I will show you command for my server on Ubuntu 14.4.
Secondly, turn on necessary apache modules:
a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel
Open conf of your domain, in my case that was a path to file:
/etc/apache2/sites-available/myDomain.pl.conf
Next, append this code
<VirtualHost>
.
.
.
RewriteEngine on
RewriteCond %{QUERY_STRING} transport=polling
RewriteRule /(.*)$ http://localhost:3001/$1 [P]
ProxyRequests off
ProxyPass /socket.io ws://localhost:3001/socket.io
ProxyPassReverse /socket.io ws://localhost:3001/socket.io
ProxyPass /socket.io http://localhost:3001/socket.io
ProxyPassReverse /socket.io http://localhost:3001/socket.io
</VirtualHost>
Finaly restart your apache
service apache2 restart
Have fun!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With