ASP.NET Core 3 Blazor: Run on localhost of server ok, but run outside server error: Error during WebSocket handshake: Unexpected response code: 200

ASP.NET Core 3 Blazor: Run on localhost of server ok, but run outside server error: Error during WebSocket handshake: Unexpected response code: 200

我正在使用 .NET Core 3.0.0,Blazor 服务器端,Visual Studio 2019,Windows Server 2016。我在服务器上 运行 正常,但来自外部服务器的连接是错误的。

我使用 Apache HTTP Web 服务器 2.4 虚拟主机 (http://httpd.apache.org/docs/2.4/vhosts/),文件 httpd-vhosts.conf

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ServerName demo.example.com
    ErrorLog ${APACHE_LOG_DIR}webapp1-error.log
    CustomLog ${APACHE_LOG_DIR}webapp1-access.log common
</VirtualHost>

httpd.conf中,启用模块:proxyproxy_httpdproxy_wstunnel

<VirtualHost *:80>
  ServerName demo.example.com

  RewriteEngine On
  RewriteCond %{REQUEST_URI}  ^/demo.example.com            [NC]
  RewriteCond %{QUERY_STRING} transport=websocket    [NC]
  RewriteRule /(.*)           ws://localhost:5000/ [P,L]

  ProxyPass / http://localhost:5000/
  ProxyPassReverse / http://localhost:5000/
</VirtualHost>

这个错误

blazor.server.js:1 WebSocket connection to 'ws://demo.xxxx.xx/_blazor?id=LZk17pmIejWGHLkhN3HFmA' failed: Error during WebSocket handshake: Unexpected response code: 200

似乎其他人在代理 Web 套接字连接时遇到了问题。在这里找到适合您的潜在解决方案:https://github.com/aspnet/Blazor/issues/1882

<VirtualHost *:80>
ServerName www.domain.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/

RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule /(.*) ws://127.0.0.1:5000/ [P]

ErrorLog /.log
CustomLog /.log common