Nginx下的.Net Core获取真实客户端IP
由于Nginx下通过代理映射道.Net Core的端口,所以直接使用 Connection.RemoteIpAddress.MapToIPv4()只能获取道127.0.0.1
要获取客户端真实IP,需要在Startup.cs或者Program.cs里面设置:
services.Configure(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
//新增如下两行
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
其中需要引用命名空间:
using Microsoft.AspNetCore.HttpOverrides;