【问题标题】:Nginx set up nodejs in port 80Nginx 在 80 端口设置 nodejs
【发布时间】:2023-09-27 16:02:01
【问题描述】:

我想将 nodejs 绑定到一个 url,像这样: http://myproject.com/nodejs/

目前,我在 8080 端口有节点。

我有 nginx 配置:

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

当我打开网址时,我得到:

Welcome to socket.io.

连接正常。

但是,当我尝试在我的网站上连接时,我得到了这个:

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

这是我用于连接的网站线路:

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

我该怎么做?

【问题讨论】:

    标签: node.js http nginx port bind


    【解决方案1】:

    我得到一个错误 =/

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

    这是我的 nginx 服务器端配置:

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

    如果我不添加:

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

    我收到此错误:

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

    我在这个链接中看到了这个配置:http://mattpatenaude.com/hosting-chatroom/

    【讨论】:

    • 您好,您有解决未知“connection_upgrade”问题的方法吗?
    • 因为我在谷歌搜索这个问题时会出现这个问题,所以我想我会在这里回答:它定义在nginx.org/en/docs/http/websocket.html
    【解决方案2】:

    之前在另一个帖子中回复过:https://*.com/a/12904282/2324004

    不幸的是,Socket.io wiki 缺少一些信息,但线索是设置资源:

    客户

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

    服务器

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

    上面的工作(我已经测试过了)。这应该适用于上面的配置:

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

    【讨论】:

      【解决方案3】:

      我从未遇到过允许从 URL 更改 socket.io 部分的配置开关。但是,AFAIK,如果您使用此配置,您的问题将得到解决:

      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/

      【讨论】:

        【解决方案4】:

        我还不能发布 cmets,但您确定您的主机名没有错字吗?

        你已经发布了一些代码片段,一切都取决于你配置了什么。

        【讨论】:

          【解决方案5】:

          只是 websocket 不起作用,如果我从 io 传输中删除 websocket,一切正常,我在客户端上使用这条线:

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

          如果我不编辑此传输列表,它可以工作,但是我有一个 websocket 错误,10 秒后,客户端使用 xhr-polling 并且它可以工作。

          【讨论】: