Nginx日志切割和记录cookie:
#!/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
#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
#设置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';