nginx日志切割和記錄cookie
[python] www.2cto.com
#!/bin/bash
#日志文件存放目錄
logs_path="/data/Service/nginx/logs"
# 日志文件的名字,多個需要空格隔開
logs_names=(dm_access)
mkdir -p ${logs_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
num=${#logs_names[@]}
for((i=0;i<num;i++))
do
#chown 和 chmod需要用到也要對當前用戶給權限,因為80端口下nginx需要sudo啟動,產生的文件所有者是root www.2cto.com
#chown ppstat:users ${logs_path}/${logs_names[i]}.log
#chmod 755 ${logs_path}/${logs_names[i]}.log
mv -f ${logs_path}/${logs_names[i]}.log \
${logs_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/${logs_names[i]}_$(date -d "yesterday" +"%Y-%m-%d").log
done
#讓nginx重新打開日志文件,在/etc/sudoer中對當前用戶添加kill權限
sudo kill -USR1 `cat $logs_path/nginx.pid`
記錄整個請求的cookie
[python]
#設置log中顯示所有cookie
set $dm_cookie "";
if ($http_cookie ~* "(.+)(?:;|$)") {
set $dm_cookie $1;
}
log_format srvmain '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$dm_cookie" $request_time';