nginx和apache日誌記錄用戶真實ip:X-Real-IP

如果結構裏有個反向代理,那後端機器的日誌記錄的就會是代理的ip,真實的ip看不到了,後端代碼可以通過在header裏設置真實ip來解決,nginx加入下面一段即可:


        proxy_set_header X-Real-IP $remote_addr;


後端通過X-REAL-IP或者HTTP_X_REAL_IP變量獲取.

日誌記錄的話,nginx可以定義$http_x_real_ip變量,例如:
    log_format main '$http_x_real_ip - $remote_user '
                      '[$time_local] "$request_method $scheme://$host$request_uri $server_protocol" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" - $remote_addr';
apache可以通過%{X-Real-Ip}i來獲取:
    LogFormat "%{X-Real-Ip}i %l %u %t \"%m
http://%v%U%q\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" - %h" combinedall

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章