Is it possible to use port 80 for both HTTP and web socket traffic? Is it possible to use port 80 for both HTTP and web socket traffic? apache apache

Is it possible to use port 80 for both HTTP and web socket traffic?


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(){...});})


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 proxya2enmod proxy_httpa2enmod 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 onRewriteCond %{QUERY_STRING} transport=pollingRewriteRule /(.*)$ http://localhost:3001/$1 [P]ProxyRequests offProxyPass /socket.io ws://localhost:3001/socket.ioProxyPassReverse /socket.io ws://localhost:3001/socket.ioProxyPass        /socket.io http://localhost:3001/socket.ioProxyPassReverse /socket.io http://localhost:3001/socket.io</VirtualHost>

Finaly restart your apache

service apache2 restart

Have fun!