简体   繁体   English

Nginx无法与Node.js + Socket.io一起使用

[英]Nginx not working with Node.js + Socket.io

I have been trying all day to get my Nginx/React applications to connect to my Node.js backend which works fine in my development environment. 我一直在努力使Nginx / React应用程序连接到我的Node.js后端,该后端在我的开发环境中可以正常工作。 My frontend and backend are deployed in their own Docker containers on the same VM. 我的前端和后端部署在同一台VM上各自的Docker容器中。 The Nginx container can communicate directly with the Node.js container, and it's confirmed working because other get/post requests work fine. Nginx容器可以直接与Node.js容器通信,并且可以确认它正常工作,因为其他get / post请求可以正常工作。

Here is was my initial Nginx configuration. 这是我最初的Nginx配置。

server {
  listen 80;

  add_header 'Access-Control-Allow-Origin' '*';
  add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }

  //This is the initial route to connect to socket.io
  location /api/notices {
    proxy_pass http://nodejs:8000;
  }

  location ~ ^/api/infoblock/[0-9a-zA-Z]+$ {
    proxy_pass http://nodejs:8000;
  }
}

This was the error I was getting in my console: 这是我在控制台中遇到的错误:

POST http://domain_name/socket.io/?EIO=3&transport=polling&t=MVFZtgP 405 (Not Allowed)

I did some more researching online, and was told to add something like this as well: 我在网上做了更多研究,并被告知也要添加以下内容:

location /socket.io/ {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_set_header Host $host;

    proxy_pass http://nodejs:8000;

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $http_host;

    proxy_http_version 1.1;
    proxy_cache_bypass $http_upgrade;
   }

When I did this, the 405 error went away, but it seems like nothing is actually happening. 当我这样做时,405错误消失了,但是似乎实际上什么都没有发生。 I'm not getting any data from the socket.io connection and nothing is being thrown in the console. 我没有从socket.io连接中获取任何数据,并且在控制台中没有抛出任何异常。

I'm not sure what I need to do. 我不确定该怎么做。 I've tried so many "solutions" I found online, and cant seem to figure out why the socket.io connection won't work, but normal get/post requests will. 我已经尝试了很多在网上找到的“解决方案”,但似乎无法弄清为什么socket.io连接不起作用,但是正常的get / post请求可以解决。

Here's how I'm connecting from the frontend, and again this is confirmed working in my development environment. 这是我从前端进行连接的方式,再次证实了这在我的开发环境中可以正常工作。

let socket = socketIOClient("/api/notices");
socket.on("NoticesReact", this._socketNotices);

How do I fix my nginx configuration to work with Node.js + Socket.io 如何修复Nginx配置以与Node.js + Socket.io一起使用

After many hours of trying to figure this out. 经过数小时的尝试来解决这个问题。 I got it, however, I'm not sure why. 我知道了,但是我不确定为什么。

This wasn't originally posted, but this is what my backend socket.io code looked like: 这不是最初发布的,但这是我的后端socket.io代码如下所示:

io.of("/api/notices").on("connection", socket => {...})

After trying just about everything I decided to remove my custom namespace, .of("/api/notices") which was a way to restrict the socket to a namespace. 在尝试了几乎所有内容之后,我决定删除我的自定义名称空间.of("/api/notices") ,这是将套接字限制为名称空间的一种方式。 This worked in development, and it documented here on socket.io . 这在开发中起作用,并在socket.io上进行了记录

So now my Nginx configuration looks like this: 所以现在我的Nginx配置看起来像这样:

server {
  listen 80;

  add_header 'Access-Control-Allow-Origin' '*';
  add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }

  location /socket.io/ {
    proxy_pass http://nodejs:8000;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
   }

  location ~ ^/api/infoblock/[0-9a-zA-Z]+$ {
    proxy_pass http://nodejs:8000;
  }
}

And magically... everything works? 神奇地……一切正常? But I have not a clue why removing my custom namespace was the solution to making it work with Nginx. 但是我不知道为什么删除我的自定义名称空间是使其与Nginx一起使用的解决方案。

If someone can figure out why removing the custom namespace worked, please feel free to comment on this. 如果有人能弄清楚删除自定义名称空间的原因,请随时对此发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM