【发布时间】:2019-12-25 06:22:20
【问题描述】:
我目前正在 Ubuntu Apache Web 服务器上使用 SignalR 运行 .Net Core 应用程序。此 .Net Core 应用可通过 example.com/api 路径访问。
我还在网络服务器上运行了一个 Vue JS 应用程序。当我尝试与 SignalR 应用程序建立 websocket 连接时,出现以下错误:
与“wss://example.com/api/mainHub?id=V3XZrhdsQDTTMIZvQ-8cbQ”的 WebSocket 连接失败:WebSocket 握手期间出错:意外响应代码:200
错误:无法启动传输“WebSockets”:null
.Net Core 应用的 Apache 配置如下:
#Main/Desired Path - https://example.com
<VirtualHost *:443>
DocumentRoot /var/www/example.com/dist/spa
ServerName example.com
#SSL Certs
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:!RC4+RSA:+HIGH:+MEDIUM:!LOW:!RC4
SSLCertificateFile /etc/apache2/ssl/example.crt
SSLCertificateKeyFile /etc/apache2/ssl/server.key
SSLCertificateChainFile /etc/apache2/ssl/example.ca-bundle
#Upgrade Websockets
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule /(.*) ws://localhost:5000/$1 [P]
#Other specific directories
ProxyPreserveHost On
ProxyPass "/api" http://localhost:5000
ProxyPassReverse "/api" http://localhost:5000
</VirtualHost>
我已经添加了 RewriteEngine 来升级 websocket,我不确定问题出在 Apache 上的配置上,还是在 .Net Core 应用程序本身上。
.Net Core 应用在startup.cs 中有以下内容
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
{
builder
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.WithOrigins("*");
}));
services.AddSignalR();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//first handle any websocket requests
app.UseWebSockets();
app.UseCors("CorsPolicy");
app.UseSignalR(routes =>
{
routes.MapHub<mainHub>("/mainHub");
routes.MapHub<accounthub>("/accountHub");
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
在建立 websocket 连接时,startup.cs 向客户端发送 200 响应似乎有问题,或者 Apache 虚拟主机未正确升级 websocket。
【问题讨论】:
-
这里的问题完全相同。你找到解决办法了吗?
标签: signalr apache2 kestrel-http-server asp.net-core-signalr kestrel