0

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 "*"
Community
  • 1
  • 1
Mark Veenstra
  • 4,691
  • 6
  • 35
  • 66
  • where did this port come from `....com:8100/...` ? i could not see it in other config?! – Yazan Dec 09 '14 at 09:56
  • I don't understand where the port 8100 comes from either. Can't find anything in the configuration files. – Mark Veenstra Dec 09 '14 at 09:57
  • what about the client config? or code? – Yazan Dec 09 '14 at 10:27
  • The client does not have any port config for port 8100. I connect the clients as described in the question – Mark Veenstra Dec 09 '14 at 10:30
  • well, i am no expert in this but i see this as only reason, and technically it's ok cuz no port 8100 is configured so it's normal to get TIME_OUT, if i were you i will focus on finding where did the 8100 came from – Yazan Dec 09 '14 at 10:34
  • @Yazan Completely true, but it is strange that I connect the clients with `io.connect('http://subdomain.mydomain.com/', {query: {extra_info : extra_info}})`, which should be port 80. – Mark Veenstra Dec 09 '14 at 10:38
  • well, i will give you a couple of things you may try, not for sure, i will add as answer, as it may not be clear as a comment – Yazan Dec 09 '14 at 10:41

2 Answers2

0

based on our comments, here is some thing you may try, not for sure.

not sure how to pass both port and extra_info query, but the idea is to set port.

io.connect('http://subdomain.mydomain.com/', {port: 80})

or this

io.connect('http://subdomain.mydomain.com:80/', {query: {extra_info : extra_info}})

good luck.

Yazan
  • 6,074
  • 1
  • 19
  • 33
  • Yazan, seems to be getting in the right direction. Bit strange, but adding the port 80 to the connection string in the client. Solves the first issue. But I no receive a `Access-Control-Allow-Origin` error. I tried to set this header in Apache, but still receiving this error. Any ideas? – Mark Veenstra Dec 09 '14 at 10:58
  • well, that's strange, though 8100 is still exist in the error, i think that caused the `Access-Control-Allow-Origin` error as accessing site on different port is considered a different domain, so cross-domain policy is applied. – Yazan Dec 09 '14 at 12:44
  • Would you have @Yazan any idea for http://stackoverflow.com/questions/27526281/websockets-and-apache-proxy-how-to-configure-mod-proxy-wstunnel? – Basj Dec 17 '14 at 20:27
0

There where 2 things going wrong. First of all, you need to specify port 80 in the client to be able to connect to port 80. So I configured the client to connect as follows:

io.connect('http://subdomain.mydomain.com:80/', {query: {extra_info : extra_info}})

After this change you will get several CORS issues. The only thing I needed to change is how the client JS file is retrieved. In my AngularJS application I included the socket.io client JS file as follows:

<script src="https://cdn.socket.io/socket.io-1.2.1.js"></script>

I needed to change this to:

<script src="http://subdomain.mydomain.com/socket.io/socket.io.js"></script>

It seems that the socket.io client is also served automatically by your socket.io server. If you retrieve the client JS from your own server you will get no CORS issues.

Mark Veenstra
  • 4,691
  • 6
  • 35
  • 66
  • i deserve 50% of the bounty :( – Yazan Dec 09 '14 at 15:50
  • oh i was kidding, you can revert if you want :) and if you keep it, many thanks! – Yazan Dec 10 '14 at 09:07
  • Maybe @MarkVeenstra you have an idea as well for http://stackoverflow.com/questions/27526281/websockets-and-apache-proxy-how-to-configure-mod-proxy-wstunnel ? I am on this topic since days... but unsuccessful – Basj Dec 17 '14 at 20:26