如果結構裏有個反向代理,那後端機器的日誌記錄的就會是代理的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