SignalR 核心 Apache WS 意外响应代码 : 200

标签 signalr apache2 kestrel-http-server asp.net-core-signalr kestrel

我目前正在 Ubuntu Apache Web 服务器上运行带有 SignalR 的 .Net Core 应用程序。此 .Net Core 应用可通过 example.com/api 路径访问。

我还有一个在网络服务器上运行的 Vue JS 应用程序。当我尝试与 SignalR 应用程序建立 websocket 连接时,出现以下错误:

WebSocket connection to 'wss://example.com/api/mainHub?id=V3XZrhdsQDTTMIZvQ-8cbQ' failed: Error during WebSocket handshake: Unexpected response code: 200

Error: Failed to start the transport '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 来升级 websockets,我不确定问题是出在 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!");
            });
        }

似乎 startup.cs 中的某些内容不正确,在建立 websocket 连接时向客户端发送 200 响应,或者 Apache 虚拟主机没有正确升级 websocket。

最佳答案

我在网站 ( http://shyammakwana.me/server/websockets-with-apache-reverse-proxy-with-ssl.html ) 上找到了这个,它似乎也对我有用。 给定站点的 apache 配置应该是这样的

<IfModule mod_ssl.c>
<VirtualHost *:443>
  RewriteEngine On
  ProxyPreserveHost On
  ProxyRequests Off

  # allow for upgrading to websockets
  RewriteEngine On
  RewriteCond %{HTTP:Upgrade} =websocket [NC]
  RewriteRule /(.*)           ws://localhost:5000/$1 [P,L]
  RewriteCond %{HTTP:Upgrade} !=websocket [NC]
  RewriteRule /(.*)           http://localhost:5000/$1 [P,L]


  ProxyPass "/" "http://localhost:5000/"
  ProxyPassReverse "/" "http://localhost:5000/"

  ProxyPass "/chatHub" "ws://localhost:5000/chatHub"
  ProxyPassReverse "/chatHub" "ws://localhost:5000/chatHub"

  ServerName site.com
  
SSLCertificateFile /etc/letsencrypt/live/site.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/site.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

您还应该启用这些模块:

a2enmod   proxy
a2enmod   proxy_wstunnel

关于SignalR 核心 Apache WS 意外响应代码 : 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57566771/

相关文章:

iis - 如何有条件地在 IIS 中禁用 Keep-Alive?

javascript - 使用 JQuery 或 Javascript 获取设备 ID

php - 403 禁止在工作模式下使用 php。仅来自浏览器的 php 文件

mod-rewrite - mod_rewrite : why can't environment variables be used to prevent recursion?

apache2 - 我安装了 apache 2,但在 sudo ufw 应用程序列表中,应用程序列表中没有 apache 应用程序

c# - 添加新包会破坏 .NET 5 应用程序

iis - 从 asp.net 核心应用程序启动外部进程 (.exe)

azure - 如何在Azure函数应用程序中使用SignalR?

asp.net - 将机器 key 添加到 web.config 时 SignalR 失败

signalr - 导出 TypeScript 模块会导致接口(interface)未被拾取