简体   繁体   中英

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 ?

I get an error =/

WebSocket connection to 'ws://myproject/socket.io/1/websocket/IomP5jiBBNv8rgGYFFUS' failed: Error during WebSocket handshake: 'Connection' header value is not 'Upgrade'

this is my nginx server side conf :

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

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 /socket.io/ {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
        }
}

if i don't add :

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

I get this error :

Restarting nginx: nginx: [emerg] unknown "connection_upgrade" variable
nginx: configuration file /etc/nginx/nginx.conf test failed

I see this conf in this link : http://mattpatenaude.com/hosting-chatroom/

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;
}

I've never met such a configuration switch allowing to change the socket.io part from URL. But AFAIK, your problem will be resolved if you use this configuration:

location /socket.io/ {
    proxy_pass http://localhost:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
}

http://michieldemey.be/blog/proxying-websockets-with-nginx-and-socket-io/

I cannot post comments yet but are you sure if you don't have a typo on hostnames?

You've posted some fragments of code, everything depends on what did you configure.

It's just the websocket don't work, if i remove websocket from io transports, all work fine, i use this line on my client :

io.transports = [ 'xhr-polling', 'jsonp-polling', 'htmlfile' ];

If i don't edit this transports list, it's work but, i have a websocket error, and after 10 sec, the client use xhr-polling and it's work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM