SignalR + Nginx + 2 Angular 应用程序在“某些”集线器上抛出 405 错误

SignalR + Nginx + 2 Angular Apps throws 405 error on `SOME` hubs

小史前:

我有 1 台服务器,1 个 Asp.Net 核心应用程序,nginx,2 个 angular 应用程序托管在同一个 Linux Debian 服务器上。 我对 SignalR 的使用如下:2 个应用程序从相同的 SignalR 集线器获取数据。我有 2 个集线器。 1 适用于 2 个应用程序,1 适用于一个应用程序,不适用于另一个应用程序(但它们使用的代码几乎相同)。我可以问一些想法为什么会弹出这个错误吗?感谢您的帮助。

P.S。一切都在本地机器上运行良好。

配置:

Asp.Net核心:

 app.UseRouting();

            // ToDo: update. This for develop only
            app.UseCors(x => x
                .SetIsOriginAllowed(_ => true)
                .AllowCredentials()
                .AllowAnyHeader()
                .AllowAnyMethod());

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

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHub<EventStageHub>("hubs/event-stage");
                endpoints.MapHub<StageActionHub>("hubs/event-stage-action");
            });

Nginx:

location /client { 
    alias /home/ignatella/evr/client/;
    try_files $uri $uri/ /client/index.html;
  }       
      
  location / { 
    alias /home/ignatella/evr/fia/;
    try_files $uri $uri/ /index.html;
  }         
           
  location /api/hubs/ {
     proxy_pass https://localhost:5001/hubs/;

     # Configuration for WebSockets
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection $connection_upgrade;
     proxy_cache off;

     # Configuration for ServerSentEvents
     proxy_buffering off;

     # Configuration for LongPolling or if your KeepAliveInterval is longer than 60 seconds
     proxy_read_timeout 100s;

     proxy_set_header Host $host;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Forwarded-Proto $scheme;
  }         
  
  location /api/ { 
    proxy_pass           https://localhost:5001/;        
    proxy_http_version   1.1;
    proxy_set_header     Host $host;
    proxy_set_header     X-Real-IP $remote_addr;
    proxy_set_header     X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header     X-Forwarded-Proto $scheme;
    proxy_set_header     X-Forwarded-Host $server_name;
  }

Angular 客户:


createHubConnection(eventId: string, modeType: string, actionTypes: string): void {
    this.hubConnection = new HubConnectionBuilder()
      .withUrl(environment.hubEventStageAction +
        `?eventId=${eventId}&stageType=${modeType}&actionTypes=${actionTypes}`, {
      })
      .configureLogging(signalR.LogLevel.Trace)
      .withAutomaticReconnect()
      .build();

    this.hubConnection.start().catch(error => console.log(error));

    this.hubConnection.on('OnActionReceived', (nodes: Node[]) => {
      ...
    });

日志:

Asp.NET核心:

info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.0 POST https://.../hubs/event-stage-action/negotiate?eventId=cb8c1cff-ff06-48d8-9771-c6479e5ffb10&stageType=chat&actionTypes=send-message&negotiateVersion=1 text/plain;charset=UTF-8 0
info: Microsoft.AspNetCore.Cors.Infrastructure.CorsService[4]
      CORS policy execution successful.
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'hubs/event-stage-action/negotiate'
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'hubs/event-stage-action/negotiate'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished HTTP/1.0 POST https://.../hubs/event-stage-action/negotiate?eventId=cb8c1cff-ff06-48d8-9771-c6479e5ffb10&stageType=chat&actionTypes=send-message&negotiateVersion=1 text/plain;charset=UTF-8 0 - 200 316 application/json 2.3196ms
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.0 GET https://.../hubs/event-stage-action?eventId=cb8c1cff-ff06-48d8-9771-c6479e5ffb10&stageType=chat&actionTypes=send-message&id=ezucHrXZ75iahsYaER3Chw - -
info: Microsoft.AspNetCore.Cors.Infrastructure.CorsService[4]
      CORS policy execution successful.
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'hubs/event-stage-action'

Nginx:

no errors in error.log. Only `status 200` responses in access.log

错误:

POST https://---/hubs/event-stage-action/negotiate?eventId=cb8c1cff-ff06-48d8-9771-c6479e5ffb10&stageType=chat&actionTypes=send-message&negotiateVersion=1 405
Utils.js:228 [2021-05-06T22:12:07.047Z] Debug: HubConnection failed to start successfully because of error 'Error'.
chat-messages.service.ts:32 Error
    at new HttpError (Errors.js:25)
    at FetchHttpClient.<anonymous> (FetchHttpClient.js:152)
    at step (FetchHttpClient.js:51)
    at Object.next (FetchHttpClient.js:32)
    at fulfilled (FetchHttpClient.js:23)
    at ZoneDelegate.invoke (zone-evergreen.js:368)
    at Object.onInvoke (core.js:28528)
    at ZoneDelegate.invoke (zone-evergreen.js:367)
    at Zone.run (zone-evergreen.js:130)
    at zone-evergreen.js:1272

解决方案:

错误的出现仅仅是因为 2 个应用程序之一中的 URI 不正确。 Conclusion: 设置路由时要小心 ))))

祝你有愉快的一天!

这看起来像是 nginx 配置文件中的位置顺序问题。 信号器集线器连接似乎尝试连接到 location / 而不是 location /api/hubs,因为所有请求都将被 index.html 捕获。尝试将 location / 部分移动到 api 部分下方。