You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SignalR结合Nginx部署双Angular应用时部分Hub返回405错误

背景说明

我在同一台Debian Linux服务器上部署了1个Asp.Net Core应用、Nginx服务和2个Angular应用。SignalR的使用场景为两个Angular应用从相同的SignalR Hub获取数据,共配置2个Hub:其中1个Hub在两个应用中都能正常工作,另1个仅在其中一个应用中正常,另一个应用连接失败(两端调用代码几乎完全一致)。本地环境下所有功能均正常,请问可能的故障原因是什么。

备注:本地开发环境所有功能运行正常。

配置信息

Asp.Net Core配置

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/;

    # WebSocket配置
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_cache off;

    # ServerSentEvents配置
    proxy_buffering off;

    # 长轮询配置/KeepAliveInterval超过60秒时的配置
    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 Core日志

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日志

error.log中无错误记录,access.log中仅存在状态码200的响应记录

错误信息

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

解决方案

该错误的根因是两个应用其中一个的请求URI配置错误。结论:配置路由时请仔细核对路径信息。

祝大家开发顺利!


内容的提问来源于stack exchange,提问作者Ignatella

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 02:27:00