Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io client-side javascript file location (with nginx reverse proxy)

I'm trying to run multiple NodeJS apps on the same server/port, using nginx as a reverse proxy which proxies to the actual running Node app.

server {
    listen       8000;
    server_name  node.domain.com;
    root         /var/www/node;

    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;

    # Enables WS support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_redirect off;

    location / {
        proxy_pass http://127.0.0.1:8001;
    }

    location /irc/ {
        proxy_pass http://127.0.0.1:8002;
    }

    location /voidwalker/ {
        proxy_pass http://127.0.0.1:8003;
    }
}

As you can see I have different projects (NodeJS apps) running (using forever) in different subdirectories of /var/www/node.

But I want to use socket.io in one of my projects. And in the socket.io docs it states that I should use <script src="/socket.io/socket.io.js"></script> to load socket.io-client on the client side, which returns a 404:

GET http://node.domain.com/socket.io/socket.io.js 404 (Not Found)

I'm guessing this is because I'm proxying the requests but I don't entirely understand how that process works internally and I would love to get this to work.

Something to note regarding this is that I have to use <link rel="stylesheet" href="/voidwalker/css/main.css"> to load my CSS, while I would rather use <link rel="stylesheet" href="/css/main.css"> in case I want to deploy it on a specific subdomain/other domain (since this is a development server).

EDIT

So, after some debugging together with Ray Stantz we think there is something going wrong in the proxy. I moved socket.io.js into /public/js/socket.io.js and I can now use it but this isn't the right solution.

If anyone has any ideas on this, I'll be pleased to hear them and try them out!

like image 491
Cas Cornelissen Avatar asked Oct 20 '22 12:10

Cas Cornelissen


1 Answers

Had the same issue. Resolved by routing /socket.io to my node.js application in the nginx configuration. In your case it should look like:

server {
    listen       8000;
    server_name  node.domain.com;
    root         /var/www/node;
    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;

    # Enables WS support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_redirect off;

    location / {
        proxy_pass http://127.0.0.1:8001;
    }

    location /irc/ {
        proxy_pass http://127.0.0.1:8002;
    }

    location /voidwalker/ {
        proxy_pass http://127.0.0.1:8003;
    }

    location /socket.io {
        proxy_pass http://127.0.0.1:8003;
    }
}

The only drawback for this solution is that only the voidwalker app can use socket.io (since /socket.io is served only on 127.0.0.1:8003).

like image 55
pive_ Avatar answered Oct 23 '22 04:10

pive_