带有 Apache 反向代理子目录的 Blazor 服务器 - WebSocket 不工作

问题描述 投票:0回答:1

我尝试在以下场景中运行标准 Blazer Server 演示(计数器):

  • 容器化(Docker)
  • 在 Debian 12 上运行
  • 在虚拟子目录中的 Apache 反向代理后面运行

应用程序无需反向代理即可正常运行。例如。当我使用它直接指定容器的端口时,如 http://myserver.example.local:8082/ 浏览器开发工具显示使用了 websocket 连接:

以下 Apache 站点配置使其能够在子目录中运行,例如 http://myserver.example.local/sd/

<VirtualHost *:80>

    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    RewriteEngine On

    <Location /sd/>

            RewriteCond %{HTTP:Upgrade} websocket [NC]
            RewriteCond %{HTTP:Connection} upgrade [NC]
            RewriteRule ^/?(.*) "ws://127.0.0.1:8082/?$1" [P,L]

            ProxyPreserveHost On
            ProxyPass http://127.0.0.1:8082/
            ProxyPassReverse http://127.0.0.1:8082/

            ProxyHTMLURLMap http://127.0.0.1:8082 /sd/
            ProxyHTMLURLMap / /sd/
            SetOutputFilter  proxy-html

    </Location>

计数器可以工作,但似乎无法建立 Websocket 连接,并且应用程序又回到长轮询状态。每个“Click Me”都会添加新的 Get 请求,如网络选项卡所示:

我该怎么做才能启用 websocket?

注意这是未进行任何更改的 Visual Studio 标准演示。我也尝试过使用 app.UsePathBase() 和 _Host.cshtml 中的 标签,但没有成功。

docker websocket server blazor reverse-proxy
1个回答
0
投票

我仍然不明白为什么上面描述的配置不起作用 - 根据很多例子,它应该起作用。

无论如何,自 2.4.47 版本起,Apache 有一个对我有用的新功能。您现在可以删除 Rewrite 部分并将“upgrade=websocket”添加到 ProxyPass 语句中。

这是一个工作配置:

<VirtualHost *:80>

DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

<Location /sd/>

        ProxyPreserveHost On
        ProxyPass http://127.0.0.1:8082/ upgrade=websocket
        ProxyPassReverse http://127.0.0.1:8082/

        ProxyHTMLURLMap http://127.0.0.1:8082 /sd/
        ProxyHTMLURLMap / /sd/
        SetOutputFilter  proxy-html

</Location>
</VirtualHost>
© www.soinside.com 2019 - 2024. All rights reserved.