Docker Nginx主机信号器Blazor Wasm应用程序:不允许使用405方法

Docker Nginx主机信号器Blazor Wasm应用程序:不允许使用405方法,docker,asp.net-core,nginx,blazor,asp.net-core-signalr,Docker,Asp.net Core,Nginx,Blazor,Asp.net Core Signalr,Nginx提供的Blazor WebAssembly应用程序的此请求返回405 Method Not Allowed错误(POST): 通过研究我发现,当Nginx作为反向代理工作时,有必要升级头部,但我的情况并非如此。这里的Nginx是一个Web服务器。这是我的nginx.conf: events {} http { include mime.types; types { application/wasm wasm; } server {

Nginx提供的Blazor WebAssembly应用程序的此请求返回405 Method Not Allowed错误(POST):

通过研究我发现,当Nginx作为反向代理工作时,有必要升级头部,但我的情况并非如此。这里的Nginx是一个Web服务器。这是我的nginx.conf:

events {}

http {
    include mime.types;
    types {
        application/wasm wasm;
    }

    server {
        listen 80;

        location / {
            root /usr/share/nginx/html;
            try_files $uri $uri/ /index.html =404;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PATCH';
            add_header 'Access-Control-Allow-Headers' 'Content-Type, X-Requested-With';
            add_header 'Access-Control-Allow-Credentials' 'true';
        }
    }
}
以下是我的后端api启动配置:

// This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddSignalR();

        services.AddCors(options =>
        {
            options.AddDefaultPolicy(builder =>
            {
                builder.AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod()
                        .SetIsOriginAllowed(host => true);
            });
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseCors();
        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<ChatHub>("/chatHub");
            endpoints.MapControllers();
        });
    }
//此方法由运行时调用。使用此方法向容器中添加服务。
public void配置服务(IServiceCollection服务)
{
services.AddControllers();
services.AddSignalR();
services.AddCors(选项=>
{
options.AddDefaultPolicy(生成器=>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed(主机=>true);
});
});
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapHub(“/chatHub”);
endpoints.MapControllers();
});
}
我错过了什么?我是否必须在nginx.conf中映射特定位置“/chatHub”才能使用WebSocket

注:我正在使用Docker容器(Docker compose)。

试试这个

...

server {
    listen 80;

        location /chatHub/ {
            proxy_pass http://localhost:4000
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
        }

        location / {
            root /usr/share/nginx/html;
            try_files $uri $uri/ /index.html =404;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PATCH';
            add_header 'Access-Control-Allow-Headers' 'Content-Type, X-Requested-With';
            add_header 'Access-Control-Allow-Credentials' 'true';
        }
    }
}

...
在docker-compose.yml上,请确保

...
ports:
  - "4000:4000"
...

所以,问题毕竟不是Nginx。我把请求发送到了错误的端口。Blazor应用程序不应该通过Nginx,而是直接向后端请求

我使用了NavigationManager类来获取后端的URI,但我没有意识到它在默认情况下指向前端的原点,这给了我错误的URI。我遵循了,但我必须做一些修改,因为我没有使用基于Asp.net的Blazor Wasm版本

但问题是端点注册的顺序集线器必须在控制器之后注册,否则浏览器将抛出CORS错误。因此,正确的答案是:

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapHub<ChatHub>("/chatHub");
        });
app.UseEndpoints(端点=>
{
endpoints.MapControllers();
endpoints.MapHub(“/chatHub”);
});