1

I have my app written in node on a Debian 7 server launched with forever and working with apache proxy via a url (without port like http.//subdomain.domain.com) with this config:

<VirtualHost *:80>
    ServerName subdomain.domain.com

    ProxyRequests off

    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>

    <Location />
            ProxyPass http://localhost:8081/
            ProxyPassReverse http://localhost:8081/
    </Location>

 </VirtualHost>

The app is running ok but the problem comes with socket.io, I´m getting this error:

GET http://192.168.1.1:8081/socket.io/?EIO=2&transport=polling&t=1424074094677-75 net::ERR_CONNECTION_TIMED_OUT

This is my server.js:

var server = express.createServer();
server.configure(function(){
    server.set('views', __dirname + '/views');
    server.set('view options', { layout: false });
    server.use(connect.bodyParser());
    server.use(express.cookieParser());
    server.use(express.session({ secret: "ezdoknahi!!"}));
    server.use(connect.static(__dirname + '/static'));
});


server.configure('development', function(){
    server.use(express.errorHandler());
});

//setup the errors
server.error(function(err, req, res, next){
    if (err instanceof NotFound) {
        res.render('404.jade', { locals: {
                  title : '404 - Not Found'
                 ,description: ''
                 ,author: ''
                 ,analyticssiteid: 'XXXXXXX'
                },status: 404 });
    } else {
        res.render('500.jade', { locals: {
                  title : 'The Server Encountered an Error'
                 ,description: ''
                 ,author: ''
                 ,analyticssiteid: 'XXXXXXX'
                 ,error: err
                },status: 500 });
    }
});
server.listen( port);

//Setup Socket.IO
var io = io.listen(server);
io.sockets.on('connection', function(socket){
  console.log('Client Connected');
  socket.on('message', function(data){
//    socket.broadcast.emit('server_message',data);
//    socket.emit('server_message',data);
      console.log(data);
  });
  socket.on('disconnect', function(){
    console.log('Client Disconnected.');
  });
});


///////////////////////////////////////////
//              Routes                   //

And this is my client app (Angular.js):

produccionApp.factory('socket', function () {
    var socket = io.connect('http://192.168.1.1:8081');
    return socket;
});

(192.168.1.1 is my subdomain.domain.com IP Address (enterprise server))

In my localhost works perfectly (how not!) but in production not. I suspect the problem is connecting through apache proxy. How to solve it?

Thanks in advance

UPDATE:

I installed nginx 1.6 on this server ( Debian 7 ) and I configured like this:

server {
    listen 80;
    server_name subdomain.domain.com;

    access_log /var/log/nginx/subdomain.domain.com.log;
    error_log  /var/log/nginx/subdomain.domain.com.error.log;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://127.0.0.1:8081/;
      proxy_redirect off;

              proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

}

And still the same error:

GET http://192.168.1.1:8081/socket.io/?EIO=2&transport=polling&t=1424074094677-75 net::ERR_CONNECTION_TIMED_OUT

I also tried to connect socket.io to http://subdomain.domain.com but still same error.

any help?

Kioko Kiaza
  • 1,378
  • 8
  • 28
  • 57
  • Did you see [that question?](http://stackoverflow.com/questions/27526281/websockets-and-apache-proxy-how-to-configure-mod-proxy-wstunnel) – Daniel Feb 18 '15 at 18:56
  • thx! I tried but my apache version is 2.2.22. I tried to upgrade to 2.4.x but there are too many packages to upgrade.... and I may broke something... – Kioko Kiaza Feb 19 '15 at 16:10
  • i think you are only attaching to port 80, and the sockets want to use 8001. what internal port is your express using ? (it just says "port")... – dandavis Feb 23 '15 at 07:54
  • hi @dandavis it is using pot 8081 – Kioko Kiaza Feb 23 '15 at 11:20

3 Answers3

1

Kioko Kiaza,

  1. The set-up is the hardest part getting thing going - you need to make your whole server code a-lot simpler - first get websockets going and then build your server.

  2. Socket.io is a bad idea, primus is much better choice all around (you can use socket.io) inside primus if you like them so much.

  3. Apache ain't a good choice for what your trying to achieve either, you should go with either nginx, or haproxy.

If you want you, can contact me on skype/email and I can help you set the whole thing up for you will later write a summary of what you learned here.

Jatin Bansal
  • 875
  • 12
  • 24
Neta Meta
  • 4,001
  • 9
  • 42
  • 67
0

I am making 2 observations here:

  1. In ngnix and Apache you are redirecting to localhost, in your code you are using IP adress. Try to keep it consistent to make bugg-finding easier.

  2. The code is bugging even though you aren't using the proxy, so it does not look like it is the redirects that is not working.

Suggestions:

  • Have you tried different approaches like using 127.0.0.1:8081? Sometimes when using the servers IP adress (specially if it is the same as the gateway), you are getting a loop-back and it can confuse the system. Eg. LAN => WAN => LAN.

  • Did you check your hosts file, to make sure that the IP addresses is right and spot on? Sometimes Linux and windows gets a stroke and dumps the hosts file and custom names such as localhost does not work anymore, i suggest you open it with vim or similar - if the file contains characters like ¤#%¤# in it, it is most likely corrupt. If not just paste all the text, delete the hosts file, create a new with the same name and paste the text in there again. Worked for me on several occasions.

  • Try and make a basic html file and place it in the root of where you want to see. Connect through the proxy and see if you get the page you made. This will tell you if there is any problems with the proxy or the service.
  • Sometimes names can be confusing, since you are using the same server as host and as proxy, try and give your service a name, and the use the hosts.txt file to handle the naming. Eg.

    # Socket io custom dns
    127.0.0.1        socket.io.service
    

    and call that through your nginix or virtualhost.

     proxy_pass http://socket.io.service:8081/;
    
Magic-Mouse
  • 603
  • 10
  • 21
  • the app is working perfectly, whats is not working is de socket.io connection. Now I think, it may use another port instead of port 80? – Kioko Kiaza Feb 25 '15 at 08:58
  • It shouldn't be necessary, try the suggestions i made event though they might not lead to a direct solution, it might give a clue about whats wrong. – Magic-Mouse Feb 25 '15 at 08:59
-1

Try listening on port 8081 instead of just 80.

Uriel Arvizu
  • 1,876
  • 6
  • 37
  • 97
  • I think his intentions is to listen to subdomain.domain.com not subdomain.domain.com:8081, what he is trying to do is direct subdomain.domain.com => localhost:8081, and if thats the case he should not change the listen. – Magic-Mouse Feb 25 '15 at 07:26