0

I got a simple node.js websocket server:

var io = require('socket.io')(12345);

io.on('connection', function(socket){
  console.log('a user connected');
  socket.on('disconnect', function(){
    console.log('user disconnected');
  });

  socket.on('test', function(msg){
    console.log('test msg: ' + msg);
    io.emit('test', 'Test answer');
  });
});

I connect to it, and it responds correctly with iocat:

me@whatever:~/prj/client$ iocat --socketio ws://localhost:12345 -e test
> adsa
> [ 'Test answer' ]

so now I want to configure apache server's ssl to redirect a certain sub-url from my project to this websocket

I have apache configured to serve my project in https://localhost/prj

I want the websocket to be avaiable in https://localhost/prj/socket

So searching the web and SO I found 2 possible ways to do this:

1 - ProxyPass alone

<Location /prj/socket>
    ProxyPass ws://localhost:12345
    ProxyPassReverse ws://localhost:12345
</Location>

2- ProxyPass with rewriting

<Location /agora/socket>
         RewriteEngine On
         RewriteCond %{REQUEST_URI}  ^/agora/socket/socket.io           [NC]
         RewriteCond %{QUERY_STRING} transport=websocket    [NC]
         RewriteRule ^/agora/socket/(.*)           "ws://localhost:12345/$1" [P,L]

         ProxyPass        /agora/socket/socket.io  ws://localhost:12345/socket.io
         ProxyPassReverse /agora/socket/socket.io  ws://localhost:12345/socket.io
</Location>

But none of them worked. The connection is established but the server doesn't respond:

me@whatever:~/prj/client$ iocat --socketio https://localhost/prj/socket -e teste
> asdfasd
> asdasd
> 

Pages where I got this info:

https://github.com/socketio/socket.io/issues/1696

https://stackoverflow.com/questions/27526281/websockets-and-apache-proxy-how-to-configure-mod-proxy-wstunnel

There is no error in the logs, it just wouldn't work. So increased the Apache LogLevel to see how the rules where being rewritten. I set it to LogLevel alert rewrite:trace6. After that I got this log in webserver connection:

[Tue Aug 01 13:22:14.510992 2017] [rewrite:trace2] [pid 28400] mod_rewrite.c(476): [client 127.0.0.1:36004] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:22:14.511023 2017] [rewrite:trace3] [pid 28400] mod_rewrite.c(476): [client 127.0.0.1:36004] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:22:14.511029 2017] [rewrite:trace1] [pid 28400] mod_rewrite.c(476): [client 127.0.0.1:36004] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] pass through /socket.io/
[Tue Aug 01 13:22:14.922563 2017] [rewrite:trace2] [pid 28401] mod_rewrite.c(476): [client 127.0.0.1:36010] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:22:14.922607 2017] [rewrite:trace3] [pid 28401] mod_rewrite.c(476): [client 127.0.0.1:36010] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:22:14.922616 2017] [rewrite:trace1] [pid 28401] mod_rewrite.c(476): [client 127.0.0.1:36010] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] pass through /socket.io/
[Tue Aug 01 13:22:15.609710 2017] [rewrite:trace2] [pid 28403] mod_rewrite.c(476): [client 127.0.0.1:36014] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:22:15.609755 2017] [rewrite:trace3] [pid 28403] mod_rewrite.c(476): [client 127.0.0.1:36014] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:22:15.609766 2017] [rewrite:trace1] [pid 28403] mod_rewrite.c(476): [client 127.0.0.1:36014] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] pass through /socket.io/
[Tue Aug 01 13:22:15.950666 2017] [rewrite:trace2] [pid 28402] mod_rewrite.c(476): [client 127.0.0.1:36020] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:22:15.950714 2017] [rewrite:trace3] [pid 28402] mod_rewrite.c(476): [client 127.0.0.1:36020] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:22:15.950724 2017] [rewrite:trace1] [pid 28402] mod_rewrite.c(476): [client 127.0.0.1:36020] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] pass through /socket.io/
[Tue Aug 01 13:22:18.329478 2017] [rewrite:trace2] [pid 28399] mod_rewrite.c(476): [client 127.0.0.1:36024] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:22:18.329512 2017] [rewrite:trace3] [pid 28399] mod_rewrite.c(476): [client 127.0.0.1:36024] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:22:18.329519 2017] [rewrite:trace1] [pid 28399] mod_rewrite.c(476): [client 127.0.0.1:36024] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] pass through /socket.io/
[Tue Aug 01 13:22:18.668606 2017] [rewrite:trace2] [pid 28424] mod_rewrite.c(476): [client 127.0.0.1:36030] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:22:18.668649 2017] [rewrite:trace3] [pid 28424] mod_rewrite.c(476): [client 127.0.0.1:36030] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:22:18.668658 2017] [rewrite:trace1] [pid 28424] mod_rewrite.c(476): [client 127.0.0.1:36030] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] pass through /socket.io/

and this when I try to send a message:

[Tue Aug 01 13:26:55.653675 2017] [rewrite:trace2] [pid 28400] mod_rewrite.c(476): [client 127.0.0.1:37454] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:26:55.653723 2017] [rewrite:trace3] [pid 28400] mod_rewrite.c(476): [client 127.0.0.1:37454] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:26:55.653734 2017] [rewrite:trace1] [pid 28400] mod_rewrite.c(476): [client 127.0.0.1:37454] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] pass through /socket.io/
[Tue Aug 01 13:26:55.987130 2017] [rewrite:trace2] [pid 28402] mod_rewrite.c(476): [client 127.0.0.1:37460] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:26:55.987173 2017] [rewrite:trace3] [pid 28402] mod_rewrite.c(476): [client 127.0.0.1:37460] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:26:55.987182 2017] [rewrite:trace1] [pid 28402] mod_rewrite.c(476): [client 127.0.0.1:37460] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] pass through /socket.io/

AFAIU the rewrite rule is being applyied. Anyone knows why the server isn't responding ?

Any help appreciated. Thanks.

0

The simplest things are often the hardest to find :)

I solved it with the 1st solution (use only ProxyPass):

<Location /prj/socket/>
        ProxyPass        http://localhost:12345/a
        ProxyPassReverse http://localhost:12345/
</Location>

The only problem was that I was using the ws:// protocol. When I changed to http:// it worked fine. Thanks to anyone who cared to take a look :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.