記錄以下
前後端分離專案需要獲取真正的客戶端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 協議》,轉載必須註明作者和本文連結