asp.net-core - ASP.NET Core 3 Blazor : Run on localhost of server ok, 但在服务器外部运行错误:WebSocket 握手期间出错:意外的响应代码:200

标签 asp.net-core blazor .net-core-3.0 blazor-server-side asp.net-core-3.0

我正在使用 .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/$1 [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/$1 [P]

ErrorLog /.log
CustomLog /.log common

关于asp.net-core - ASP.NET Core 3 Blazor : Run on localhost of server ok, 但在服务器外部运行错误:WebSocket 握手期间出错:意外的响应代码:200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58196221/

相关文章:

asp.net-core-3.0 - 在 .Net core 3.0 控制台应用程序中使用 HttpClient 上传文件

mysql - .NET Core 添加迁移忽略一些属性

c# - 如何在 EF Core 3 中启用日志记录?

asp.net-core - 无法将 JSON 发布到 ASP.NET Core RazorPage 处理程序

javascript - 使用 JSInterop 将对象传递给 JavaScript 结果为空对象

c# - 如何在 Visual Studio 2019 的 Blazor 中向组件添加分部类?

c# - 我如何在 .net core 3.0 中执行一次 worker 服务?

c# - ASP.NET Core 3.0 身份不向我的浏览器添加任何身份验证数据

c# - 如何调用需要.Net Core签名的XML SOAP服务?

c# - 部署项目时无法访问 'wwwroot' 中的文件