【问题标题】:Nginx Websocket Configuration for Dual Proxies双代理的 Nginx Websocket 配置
【发布时间】:2020-01-25 05:08:51
【问题描述】:

我正在尝试通过两个 nginx 代理发出 websocket 请求。就是做SSR(服务端渲染)

我的堆栈是这样的: 外部世界 nginx rendora(SSR) nginx daphne django

当我在做 外部世界 nginx daphne django

Websocket 连接建立得非常好。但是当我实现 rendora(SSR) 并向 nginx 添加另一个代理时,它不起作用。握手时 Websocket 连接失败。我猜“升级”请求在我的 nginx 服务器中某处失败。我的 nginx 配置如下:

server {
        listen 8000;

        location / {
                include proxy_params;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_pass http://unix:/home/ubuntu/myproject/myproject.sock;
        }
}

server {
        server_name mydomain;
        charset utf-8;

        location /static {
                alias /home/ubuntu/myproject/apps/web-build/static;
        }
        location /      {
                include proxy_params;
                proxy_pass http://127.0.0.1:3001;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }

    listen 443 ssl; # managed by Certbot
   ...
}

因此位置/端口 443 将来自外部世界的所有请求发送到 127.0.0.1:3001,rendora(SSR 服务器)正在侦听。并且 SSR 服务器 (rendora) 将它们转发到 127.0.0.1:8000 那里 nginx 代理连接到 daphne unix 套接字。

除了 websocket 请求之外,SSR 本身运行良好。 但是我不知道为什么当请求必须通过两个 nginx 代理时没有完成 websocket 升级。

【问题讨论】:

    标签: nginx websocket


    【解决方案1】:

    我通过将 /graphql websocket 请求直接发送到 Daphne 服务器解决了这个问题。当我将它们路由到另一个 nginx 代理服务器时,我仍然不知道为什么 websocket 连接不起作用。

    location /graphql {
                            include proxy_params;
                            proxy_pass http://unix:/home/ubuntu/myproject/myproject.sock;
                            proxy_http_version 1.1;
                            proxy_set_header Upgrade $http_upgrade;
                            proxy_set_header Connection "upgrade";
                        }
    
    location /      {
              include proxy_params;
              proxy_pass http://127.0.0.1:3001;
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 2019-05-13
      • 2021-07-09
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多