.NET 6 Web API部署Google Cloud Run IIS容器后413请求过大问题
解决部署到Windows IIS的Google Cloud Run容器后.NET 6 Web API的413 Payload Too Large错误
以下是针对该问题的排查和解决步骤:
配置Google Cloud Run的请求大小限制
Cloud Run默认的最大请求大小为32MB,即便你已经放开IIS和应用本身的限制,Cloud Run的网关仍会拦截超出该阈值的请求。需要在部署服务时调整此限制:gcloud run deploy YOUR_SERVICE_NAME --image YOUR_IMAGE_URL --max-request-size=1G或者在Google Cloud控制台的服务设置中,进入「容器、变量、秘密和连接」→「容器」标签,手动设置「最大请求大小」为你需要的数值(比如1G)。
完善IIS的web.config配置
除了requestFiltering,还需要确保aspNetCore节点的配置没有额外限制,同时延长请求超时时间避免大文件上传超时:<system.webServer> <aspNetCore processPath="dotnet" arguments=".\YourApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" requestTimeout="00:20:00" /> <security> <requestFiltering> <requestLimits maxAllowedContentLength="4294967295" /> </requestFiltering> </security> </system.webServer>确认IIS集成模式下的应用配置优先级
在IIS集成模式下,需确保IISServerOptions配置正确生效,同时保留Kestrel的请求大小设置,并添加正向代理头部支持(适配Cloud Run的反向代理场景):builder.WebHost.UseKestrel(options => { options.Limits.MaxRequestBodySize = long.MaxValue; }); builder.Services.Configure<IISServerOptions>(options => { options.MaxRequestBodySize = int.MaxValue; }); // 启用正向代理头部支持,确保请求信息正确传递到后端API app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto });排查自定义中间件或第三方库的限制
检查Web API中是否存在自定义中间件、Swagger或认证类库对请求大小做了额外限制,逐一排查并调整对应配置。
内容的提问来源于stack exchange,提问作者DavidJS
相关产品推荐
相关产品推荐

