人本善良

导航

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();
    }

posted on 2024-08-15 13:32  简简单单2018  阅读(20)  评论(0编辑  收藏  举报