ai-fix: proxy problems

This commit is contained in:
xiaolipro 2026-04-17 11:35:28 +08:00
parent eef3e974fb
commit b78cce31fd

View File

@ -75,11 +75,12 @@ public class InterfaceForwardApiModule : FakeModule
}); });
}); });
//Nginx代理的话获取真实IP
services.Configure<ForwardedHeadersOptions>(options => services.Configure<ForwardedHeadersOptions>(options =>
{ {
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; // 反向代理场景下恢复客户端真实来源信息,供 Swagger、鉴权和重定向等中间件使用
//新增如下两行 options.ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto |
ForwardedHeaders.XForwardedHost;
options.KnownNetworks.Clear(); options.KnownNetworks.Clear();
options.KnownProxies.Clear(); options.KnownProxies.Clear();
}); });
@ -94,6 +95,9 @@ public class InterfaceForwardApiModule : FakeModule
{ {
var app = context.GetWebApplication(); var app = context.GetWebApplication();
// 必须在读取 Scheme/Host/PathBase 的中间件之前执行,否则 Swagger 会生成错误的请求地址
app.UseForwardedHeaders();
app.Use(async (httpContext, next) => app.Use(async (httpContext, next) =>
{ {
httpContext.Request.EnableBuffering(); httpContext.Request.EnableBuffering();
@ -118,8 +122,6 @@ public class InterfaceForwardApiModule : FakeModule
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
app.UseForwardedHeaders();//Nginx代理的话获取真实IP
app.MapControllers(); app.MapControllers();
app.Services.GetRequiredService<IFeiShuNotificationService>().Send("Application started"); app.Services.GetRequiredService<IFeiShuNotificationService>().Send("Application started");