SignalR Core Apache WS意外的响应代码:200

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

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

我也有一个在网络服务器上运行的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
1个回答
0
投票

在将ASP.NET Core 3.1 Blazor服务器端应用程序部署到运行Apache2的Ubuntu 18.04服务器时,我也遇到此错误。

结合此错误,尝试从Linux服务器访问静态资源(.css文件,图像和脚本)时,我也遇到404错误,但在IIS环境中一切正常。我还注意到,发布应用程序时,解决方案中WWWRoot文件夹的内容似乎未复制到linux服务器。我正在使用发布的FolderProfile方法,然后通过SFTP将发布文件夹的内容复制到linux服务器。

[从样板启动以来唯一的“真实”更改。配置类(来自VS2019中的New Blazor-Server Web App模板)是我按照https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-3.1中的说明添加UseForwardHeaders指令的>

我还向应用程序中添加了身份验证和授权,但是再次,整个过程可以在IIS上完美运行,但是一旦测试完成,就需要将其安装在生产Linux服务器上。

我的Startup.Configure看起来像:

 public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            // Enable SyncFusion Community Edition License
            Syncfusion.Licensing.SyncfusionLicenseProvider
                .RegisterLicense("Key-Removed");

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();


            // Use Authentication and Authorisation providers in the application
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }

此外,在Linux端,虚拟主机配置文件如下所示:

#<VirtualHost *:*>
#    RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
#</VirtualHost>

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ServerName www.grafton.internal
    ErrorLog ${APACHE_LOG_DIR}labapp_error.log
    CustomLog ${APACHE_LOG_DIR}labapp_access.log common
</VirtualHost>

注意,我已经注释掉了前三行,因为我无法使该表达式正常工作-Apache无法以我尝试的第二行的每个变体开头。我相信这可能与我的问题有关,但是我不确定该怎么办。

我还想强调一点,该应用程序仅供内部使用,并且Web服务器仅在我们的Intranet上可见-没有面向外部/ Internet的服务器或站点,并且只有极少数人可以通过物理访问来访问服务器和受限制的固定IP地址,因此我完全不关心使用SSL。

对此提供的任何帮助将不胜感激。我一直在努力使它运行一个星期,仅剩一个星期才必须在Linux测试环境中进行。

© www.soinside.com 2019 - 2024. All rights reserved.