Reverse proxy'ing of socket.io events not working

Leonard Theivendra theivend at ca.ibm.com
Mon Jun 3 18:55:52 UTC 2019



Hi, I've been trying to get nginx reverse proxy'ing of socket.io events
working but not having any luck so would greatly appreciate any
help/insight. Basically my scenario is: nginx running in a docker container
(based on the nginx:stable-alpine image), a node server app serving up rest
APIs as well as responses via socket.io when those APIs are called. On the
client side, the aim is to call these APIs via the proxy, and also listen
to socket.io events return back through the proxy. And this is all ssl
secured.

My nginx conf file:

worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type  application/octet-stream;
    access_log  /var/log/nginx/access.log  main;
    sendfile on;
    keepalive_timeout  65;

    server {
        listen 8080 ssl;

        server_name localhost;

        ssl_certificate /etc/nginx/conf.d/cert.pem;
        ssl_certificate_key /etc/nginx/conf.d/key.pem;

        location / {
            proxy_pass https://apiandsocketservername:9091;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}


My simple socket.io client node app:

socket = require('socket.io-client')(`https://localhost:8080`, {timeout:
5000, rejectUnauthorized: false});
// Connect to the socket
socket.on('connect', () => {
    console.log('Connected to server');
});
// Wait on event named 'projectValidated'
socket.on('projectValidated', (args) => {
    console.log('projectValidated');
    console.log(args);
});


So the scenario would be to start up the node app above, then hit the rest
API endpoint on https://localhost:8080 via curl, and listen for the socket
event on https://localhost:8080 originating from
https://apiandsocketservername:9091 via the proxy.

When I run the nginx container and socket.io client locally on my laptop
everything works, and the nginx container logs has this added line when the
socket.io node app client connects, and no extra logging when the socket.io
events are successfully received:
172.17.0.1 - - [31/May/2019:16:31:07 +0000]
"GET /socket.io/?EIO=3&transport=polling&t=MiEkmaT&b64=1&sid=viHdGa9b26g9ejGYAAAD
 HTTP/1.1" 200 3 "-" "nodeXMLHttpRequest" "-"
When I kill the socket.io client, nginx emits this single line in the log:
172.17.0.1 - - [31/May/2019:16:34:18 +0000]
"GET /socket.io/?EIO=3&transport=websocket&sid=viHdGa9b26g9ejGYAAAD
HTTP/1.1" 101 379 "-" "-" "-"


But when I run the same scenario on a kubernetes cluster where the nginx
proxy container and the socket.io client is on one pod and the rest api and
socket.io server is on a different pod: all the rest api proxy'ing works
for the both the https api calls and responses, and also the node app is
able to do the socket connection properly, but the socket.io event is never
received successfully. Nginx also continuously emits these two lines in the
log every 25 seconds:
9.37.248.25 - - [31/May/2019:17:37:26 +0000]
"GET /socket.io/?EIO=3&transport=polling&t=MiEzrxs&b64=1&sid=UJnUsScsSyJSmrOiAAAA
 HTTP/1.1" 200 3 "-" "node-XMLHttpRequest" "-"
9.37.248.25 - - [31/May/2019:17:37:26 +0000]
"POST /socket.io/?EIO=3&transport=polling&t=MiEzy2V&b64=1&sid=UJnUsScsSyJSmrOiAAAA
 HTTP/1.1" 200 2 "-" "node-XMLHttpRequest" "-"


Would anyone be able to shed light on what could be going wrong and if I
need to change anything in my nginx conf file? And what the above two lines
repeatedly emitted in the nginx log mean?

Thanks in advance,
Len.


---------------------------------------------
Len Theivendra
IBM Canada Software Lab
email: theivend at ca.ibm.com
tel: (905) 413-3777  tie: 969-3777
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20190603/b81f3b0c/attachment.html>


More information about the nginx mailing list