nginx使用realip模塊獲取用戶的真實ip

我們經常會使用cdn來達到加快網站訪問速度和隱藏服務器真實ip的目的,但是站點使用了cdn后程序獲取到的用戶ip以及nginx日志中記錄的ip均會變成cdn的中轉ip。不過cdn一般會實用自定義ip頭來保存用戶的真實ip,或者是將其放在X_FORWARDED_FOR頭里,通過nginx的realip模塊和這些ip頭里的信息就可以獲取到用戶的真實ip了。

首先需要確認安裝nginx的時候加上了realip模塊:

./configure --with-http_realip_module

nginx配置示例:

    server {
    listen       80;
    server_name  www.test.com;
    index index.php index.html index.html;
    root /data/site/www.test.com;
    access_log  /data/wwwlogs/test.access.log  main;
 
    set_real_ip_from  192.168.50.0/24;
    set_real_ip_from  61.22.22.22;
    set_real_ip_from  121.207.33.33;
    set_real_ip_from  127.0.0.1;
    real_ip_header    X-Forwarded-For;
    real_ip_recursive on;
    fastcgi_pass  unix:/var/run/phpfpm.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
}


轉載請注明出處 AE博客|墨淵 ? nginx使用realip模塊獲取用戶的真實ip

發表評論

路人甲

網友評論(0)