Nginx 反向代理后如何获取真实客户端 IP
记录以下
前后端分离项目需要获取真正的客户端IP 【需要在nginx 设置如何几个地方】
1 vhost 中的虚拟主机任意处
location /api {
rewrite ^/api/(.*)$ /api/$1 break;
proxy_pass https://***.*********.com;
}
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2 如果有报错
Starting nginx: nginx: [warn] could not build optimal proxy_headers_hash, you should increase either proxy_headers_hash_max_size: 512 or proxy_headers_hash_bucket_size: 64; ignoring proxy_headers_hash_bucket_size
nginx.conf 下的http 模块下设置
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
3 代码里面获取
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: