2
votes

I'm trying to connect to a socket.io server running behind a Nginx reverse proxy. I have Nginx proxy server running with https config https://api.xxx.com. The node js socket.io server is running on another server with http config http://a.b.c.d:8081/app/v2.

In android side as a socket client, I'm using implementation 'com.github.nkzawa: socket.io-client:0.3.0.

Proxy is configured like this :

location /app/v2 {

            proxy_cache apicache;
            proxy_cache_revalidate on;
            proxy_cache_min_uses 2;
            proxy_cache_use_stale error timeout updating http_500 http_502
                          http_503 http_504;
            proxy_cache_lock on;
            proxy_cache_bypass $http_cache_control;
            add_header X-Proxy-Cache $upstream_cache_status;
            proxy_cache_methods GET;
            proxy_cache_valid 200 302 2s;
            proxy_cache_valid 404 1m;

            proxy_pass http://a.b.c.d:8081/app/v2;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;

    }

Node js Server :

socketio = socketio.listen(server);
socketio.set('transports', ['websocket']);
socketio.of('/app/v2/socketio/livefeed/').on('connection', function (socket) {
    console.log("device connected");
    socket.emit('clientAction','connected');
    socket.on('disconnect', function () {
        console.log("device disconnected");
        socket.emit('clientAction','disconnected');
    });
});

While trying to connect the android app to socket like this

`try { 
    mSocket = IO.socket("https://api.xxx.com/app/v2/socketio/livefeed/");
 } catch (URISyntaxException e) { 
    Log.d(TAG, String.valueOf(e)); 
 } 
 mSocket.connect();`

The socket client is not connecting to the socket.io server. How should I connect android socket client to socket server behind the proxy server?

2
Provide your complete nginx config - Tarun Lalwani
updated the nginx.conf @TarunLalwani - baldraider
If you connect directly to http://a.b.c.d:8081/app/v2, does it work? - Tarun Lalwani
Yes by directly connecting to url works perfectly. I also added the server socket code @TarunLalwani - baldraider
Can you change location /app/v2 { to location /app/v2/socketio { and append the same in proxy_pass also and see if it works? - Tarun Lalwani

2 Answers

0
votes

The url you're using is the issue, since you might need to pass it to proxy anyways for your routing purposes please try the following in your nginx conf. Hope this helps.

location ~ /app/v2/(?<section>.*) {

        proxy_cache apicache;
        proxy_cache_revalidate on;
        proxy_cache_min_uses 2;
        proxy_cache_use_stale error timeout updating http_500 http_502
                      http_503 http_504;
        proxy_cache_lock on;
        proxy_cache_bypass $http_cache_control;
        add_header X-Proxy-Cache $upstream_cache_status;
        proxy_cache_methods GET;
        proxy_cache_valid 200 302 2s;
        proxy_cache_valid 404 1m;

        proxy_pass http://a.b.c.d:8081/app/v2/$section;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;



}
0
votes

I've faced such problem with sockets.

In my case, it was solved by adding this in nginx.conf

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}