简体   繁体   English

使用 Nginx 和 Docker 向 CRA 开发服务器使用 proxy_pass 时出现 Websocket 错误

[英]Websocket error when using a proxy_pass to CRA development server using Nginx and Docker

I am trying to proxy_pass in Nginx to my React dev server at localhost:3000 .我正在尝试将 Nginx 中的proxy_pass传递到位于localhost:3000的 React 开发服务器。 It currently works but I am seeing an error in my console which I am unsure will cause any issues down the road:它目前可以工作,但我在控制台中看到一个错误,我不确定这会导致任何问题:

WebSocketClient.js:16 WebSocket connection to 'ws://localhost/ws' failed: WebSocketClient.js:16 WebSocket 连接到 'ws://localhost/ws' 失败:

Nginx.conf : Nginx.conf

upstream backend {
    server backend:8000;
}

upstream frontend {
    server frontend:3000;
}

server {
    listen 80;
    
    location / {
        proxy_pass http://frontend$request_uri;
        proxy_set_header Host $http_host;
    }
    
    location /api/ {
        proxy_pass http://backend;
        proxy_set_header Host $http_host;
    }

    location /admin/ {
        proxy_pass http://backend;
        proxy_set_header Host $http_host;
    }
}

I have tried adding WDS_SOCKET_PORT=0 to the frontend .env file also tried adding:我尝试将WDS_SOCKET_PORT=0添加到前端.env文件中,还尝试添加:

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";

...as suggested in other SO posts. ...正如其他 SO 帖子中所建议的那样。 Neither worked.都没有奏效。

What is this error?这是什么错误? How can I fix it?我该如何解决?

I solved this by adding more in the variables我通过在变量中添加更多来解决这个问题

environment:
  - WDS_SOCKET_PORT=443

Maybe it can help you, but if you haven't proxy between, use WDS_SOCKET_PORT=0 in docker-compose.yml也许它可以帮助你,但如果你没有代理,请在 docker-compose.yml 中使用 WDS_SOCKET_PORT=0

Try this:尝试这个:

environment:
  - WDS_SOCKET_PORT=443

It works for me这个对我有用

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

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