nginx 上的 Socket.io 404

Itz*_*Pig 4 nginx node.js socket.io

所以我查了很多帖子、网站,仍然没有解决这个问题。

我之前在我的服务器上运行过一个项目,包括socket.io,没有任何问题。

但是现在我将这个新项目上传到服务器,socket.io 似乎总是返回 404。

抱歉,如果我在这里忽略了一些明显的事情。

Nginx 配置:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    index index.html index.htm index.nginx-debian.html;

    server_name localhost;

    location / {
        proxy_pass http://localhost:3001;
        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;
    }

    location /demo/ {
        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_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_cache_bypass $http_upgrade;
    }

    location /socket.io/ {
        proxy_pass http://localhost:3000/;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass_request_headers on;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_cache_bypass $http_upgrade;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的socket.io服务器:

const app = express();
const server = http.createServer(app);
const io = require('socket.io')(server);
server.listen(3000);
Run Code Online (Sandbox Code Playgroud)

客户(哈巴狗):

script(src="/socket.io/socket.io.js")
Run Code Online (Sandbox Code Playgroud)

请注意,上面的代码是我在阅读了这里关于服务器故障的大量问题以及几个地方的博客文章后得到的结果。

我不记得在我使用的第一个位置的socket.io 中使用了第二个位置“标签”(是它的名字吗?)。

小智 5

代理通行证应该是:

location /socket.io/ {
  proxy_pass http://localhost:3000/socket.io/;
  ... 
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅https://medium.com/@ibraheemabukaff/how-to-proxy-websockets-with-nginx-e333a5f0c0bb ...