Nginx在端口80中设置nodejs

 lj劲-Ybw_582 发布于 2023-02-13 18:15

我想绑定nodejs到一个网址,如下所示: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/');

我怎样才能做到这一点 ?

1 个回答
  • 我收到错误=/

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

    这是我的nginx服务器端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;
            }
    }
    

    如果我不添加:

        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/

    2023-02-13 18:19 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有