On our server we are running Apache already on port 80, on this same server I want to serve socket.io and I would like to connect to the socket.io server also on port 80. So I tried to get Apache to proxy port 80. I found this stackoverflow entry: https://serverfault.com/questions/616370/configuring-apache-2-4-mod-proxy-wstunnel-for-socket-io-1-0
What I did:
I enabled the next modules in apache:
a2enmod rewrite proxy proxy_wstunnel
I created a new site in '/etc/apache2/sites-available/mysite.conf', with the next content:
<VirtualHost subdomain.mydomain.com:80>
Servername subdomain.mydomain.com
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:3001/$1 [P,L]
ProxyPass /socket.io http://localhost:3001/socket.io
ProxyPassReverse /socket.io http://localhost:3001/socket.io
</VirtualHost>
On the same server I have the socket.io server running on port 3001:
server.listen('3001', function(){
console.log('Socket.io server listening on *:3001');
});
io.listen(server).on('connection', function(client) {
// Rest of the code
}
And the socket.io clients are connecting on this subdomain configured into apache:
var socket = io.connect('http://subdomain.mydomain.com/', {query: {extra_info : extra_info}});
Once I startup a client. I don't see anything coming into the socket.io server logging (I have added a lot of console.log() entries. All I see in the console of the client is the next error:
GET http://subdomain.mydomain.com:8100/socket.io/?extra_info=fe4567&EIO=3&transport=polling&t=1416987210079-31 net::ERR_CONNECTION_TIMED_OUT
If I connect the clients directly to port 3001 of the socket.io server http://subdomain.mydomain.com:3001
all is just working fine (when I open port 3001 on the apache server).
I am running the next versions:
- Apache: 2.4.7
- Socket.io server & client: 1.2.1
EDIT:
When I change the connection URL in var socket = io.connect('http://subdomain.mydomain.com/', {query: {extra_info : extra_info}});
to http://subdomain.mydomain.com:80
.
I receive a different error:
GET http://subdomain.mydomain.com/socket.io/?user_id=2&EIO=3&transport=polling&t=1418122378582-40
(index):1 XMLHttpRequest cannot load http://subdomain.mydomain.com/socket.io/?extra_info=sdferr&EIO=3&transport=polling&t=1418122378582-40. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.178.158:8100' is therefore not allowed access. The response had HTTP status code 500.
I though setting the Access-Control-Allow-Origin
header in Apache would solve the issue, but it doesn't. I have added this to the file /etc/apache2/sites-available/mysite.conf
:
Header set Access-Control-Allow-Origin "*"