【问题标题】:forward websocket to websocket将 websocket 转发到 websocket
【发布时间】:2020-12-26 01:38:00
【问题描述】:

我使用 nginx 作为 django 的反向代理,并使用 as config 进行反应

worker_processes  1;
events {
    worker_connections  1024;
}
http{
    server{
        include mime.types;
        default_type application/octet-stream;
        keepalive_timeout  240;
        sendfile on;
        listen 8001;
        server_name 127.0.0.1;

        location /{
            proxy_pass http://localhost:3000;
        }

        location /backend {
                proxy_pass http://127.0.0.1:8000;
        }   
 }
}

它工作正常,但我想转发 websocket 以响应热加载。经过大量谷歌搜索后,我仍然没有解决方案。目前它有来自 chrome 控制台的连接错误

WebSocket connection to 'ws://127.0.0.1:8001/sockjs-node' failed: Error during WebSocket handshake: Unexpected response code: 404

【问题讨论】:

    标签: nginx websocket


    【解决方案1】:

    由于 http 指令,我认为它不会支持转发 WebSocket 代理,但在 google 上花费了更多时间。我知道 http 在初始握手后升级到 Websocket 所以最后解决方案就在这里

    它对我来说是 Django 作为后端的转发代理并作为前端做出反应。所以我可以通过 CORS 问题,因为服务器位于不同的 IP 并且它不安全,所以设置标头不支持太多关于 cookie 共享

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http{
        client_max_body_size 100M;
        server{
            include mime.types;
            default_type application/octet-stream;
            keepalive_timeout  240;
            sendfile on;
            listen 8001;
            server_name 127.0.0.1;
    
            location /{
                proxy_pass http://localhost:3000;
            }
    
            location /backend {
                    proxy_pass http://127.0.0.1:8000;
            }   
            location /sockjs-node {
                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;
            }
     }
    }
    
    
    

    【讨论】:

      猜你喜欢
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      • 2022-09-24
      • 2016-01-08
      • 2013-03-02
      • 2023-03-19
      • 2017-10-25
      相关资源
      最近更新 更多