Nginx 设置获取真实IP
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://your_backend_server;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
string ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
if (string.IsNullOrEmpty(ip))
{
ip = context.Request.Headers["X-Real-IP"].FirstOrDefault();
}
if (string.IsNullOrEmpty(ip))
{
ip = context.Connection.RemoteIpAddress?.ToString();
}