Modified by D 的博客

当 nginx 代理时,ASP.NET Core 2.1 的 RemoteIpAddress 无法获取客户 IP 解决方法

1. 配置 nginx

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;

2. 在 ASP.NET Core 的 Startup.cs 的 Configure 中添加代码

var fordwardedHeaderOptions = new ForwardedHeadersOptions
    {
        ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
    };
fordwardedHeaderOptions.KnownNetworks.Clear();
fordwardedHeaderOptions.KnownProxies.Clear();
app.UseForwardedHeaders(fordwardedHeaderOptions);

发表评论

评论列表,共 0 条评论

    暂无评论