Nginx在80端口设置nodejs

pes8fvy9  于 2023-06-28  发布在  Nginx
关注(0)|答案(6)|浏览(142)

我想把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;
            }
}

当我打开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/');

我该怎么做?

1l5u6lss

1l5u6lss1#

我得到一个错误=/

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/

dojqjjoe

dojqjjoe2#

之前在另一个帖子中回复过:https://stackoverflow.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;
}
q9yhzks0

q9yhzks03#

我从来没有遇到过这样的配置开关,允许从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/

sirbozc5

sirbozc54#

server {
    listen 80;
    server_name domain name;
    access_log /var/log/nginx/dev_localhost.log;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
ou6hu8tu

ou6hu8tu5#

我还不能发表评论,但你确定你没有主机名上的错字吗?
你已经发布了一些代码片段,一切都取决于你配置了什么。

sc4hvdpw

sc4hvdpw6#

这只是WebSocket不工作,如果我从io传输中删除websocket,所有工作正常,我在我的客户端上使用这行:

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

如果我不编辑这个传输列表,它是工作的,但是,我有一个WebSocket错误,10秒后,客户端使用xhr轮询,它的工作。

相关问题