Search code examples
node.jshttpnginxportbind

Nginx set up nodejs in port 80


i want bind nodejs to a url, like this: http://myproject.com/nodejs/

Currently, i have node in port 8080.

And i have nginx configuration :

upstream app {
    server 127.0.0.1:8080;
}    
server {
    listen   80; ## listen for ipv4; this line is default and implied

    root /home/myproject/www;
    index index.html index.htm;

    server_name myproject.com;

            location /nodejs/ {
                  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://app/;
                  proxy_redirect off;
            }
}

When i open the url i get :

Welcome to socket.io.

The connection is ok.

But, when i try to connect in my website, i get this :

GET http://myproject.com/socket.io/1/?t=1385767934694 404 (Not Found) socket.io.js:1659

this is my website line for do the connection :

    var socket = io.connect('http://myproject.com/nodejs/');

How can i do that ?


Solution

  • It was replied earlier in another thread: https://stackoverflow.com/a/12904282/2324004

    Unfortunately, Socket.io wiki lacks of some of information but the clue is to set up resource:

    Client

    var socket = io.connect('http://localhost:8081', {resource: 'test'});
    

    Server

    var io = require('socket.io').listen(8081, {resource: '/test'});
    

    Above is working (I've tested it at the moment). This one should work with the configuration above:

    location /test/ {
        proxy_pass http://localhost:8081;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }