我用的是lnmp.org的環境
/usr/local/nginx/conf/nginx.conf
在
http {
}
部分增加
map $HTTP_CF_CONNECTING_IP $clientRealIp {
"" $remote_addr;
~^(?P<firstAddr>[0-9.]+),?.*$ $firstAddr;
}
log_format main '$clientRealIp [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'$http_user_agent $remote_addr $request_time';
主要是為了通用性,如果關閉了CDN,可以不需要修改獲取IP的方式,所以才這么修改,不然直接用$HTTP_CF_CONNECTING_IP就行了(這個時候就不需要在日志格式里使用$clientRealIp)
然后在網站記錄的日志定義使用main這個日志格式
比如
access_log /home/wwwlogs/abc.com.log main;