node.js - Nginx 在 80 端口设置 nodejs

标签 node.js http nginx port bind

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

目前,我的 Node 在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;
            }
}

当我打开 url 时,我得到:

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/');

我该怎么做?

最佳答案

我得到一个错误 =/

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

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

关于node.js - Nginx 在 80 端口设置 nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20294775/

相关文章:

node.js - 为 Node.js TCP 服务器创建 REST API 包装器

spring-boot - 如何从 openssl 生成的 key 生成 X.509 证书

node.js - http服务器中的NodeJS err-ssl-protocol-error

javascript - NodeJS 项目没有添加到对象的索引上?

node.js - 我可以在没有 Apollo Server 的情况下使用 Apollo 客户端吗?我有一个普通的 GraphQl Express 服务器,想要与前端 ReactJS 集成

rest - 如何调试 Whook 的 `E_NOT_FOUND` 错误?

java - 将 HTTP 请求重定向到不同的服务器

node.js - 如何设置在 "npm install"期间使用的环境变量?

ubuntu - nginx 似乎会自行重新编译

.net - .NET 平台文件扩展名是什么?