Nginx:日志配置与日志定时分割备份
Posted 陌生谁家年少
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx:日志配置与日志定时分割备份相关的知识,希望对你有一定的参考价值。
1.日志格式设置
- log_format
log_format main '$server_name $remote_addr - $remote_user [$time_local] "$request" '
'$status $upstream_status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$ssl_protocol $ssl_cipher $upstream_addr $request_time $upstream_response_time';
- 配置
http
##
# Logging Settings
##
log_format main '$server_name $remote_addr - $remote_user [$time_local] "$request" '
'$status $upstream_status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$ssl_protocol $ssl_cipher $upstream_addr $request_time $upstream_response_time';
# access_log off;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
- 输出
localhost 171.168.1.3 - - [29/Jul/2020:14:16:12 +0800] "GET /nginx/xxx/xx/x?sign=xxxxx HTTP/1.1" 200 200 103 "https://xxx.example.com.cn/nginx/xx/xx/web?type=register&code=043tmemb1loe2A0h7lmb1HQemb1tmemF&state=123" "Mozilla/5.0 (Linux; android 9; CLT-AL00 Build/HUAWEICLT-AL00; wv) AppleWebKit/537.36 (Khtml, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045227 Mobile Safari/537.36 MMWEBID/1269 MicroMessenger/7.0.17.1720(0x27001135) Process/tools WeChat/arm64 NetType/WIFI Language/zh_CN ABI/arm64" "171.168.1.251:59808" - - 171.168.1.5:8070 0.021 0.020
2.日志定时分割脚本
- backup_log_nginx.sh
创建脚本,脚本为分割昨天的日志
#!/bin/bash
PATH_LOG=/var/log/nginx
YESTERDAY=$(date -d "yesterday" +%Y%m%d)
mv $PATH_LOG/access.log $PATH_LOG/access_$YESTERDAY.log
#USR1
kill -USR1 `ps axu | grep "nginx: master process" | grep -v grep | awk 'print $2'`
exit 0
- crontab
配置定时任务,每天0点执行日志分割脚本
#每天0点0分执行/root/devtools/backup_log_nginx.sh
0 0 * * * /root/devtools/backup_log_nginx.sh
3.效果
[root@MQ-SERVER nginx]# ls -l
总用量 1451624
-rw-r--r-- 1 nobody root 256285165 7月 31 00:00 access_20200730.log
-rw-r--r-- 1 nobody root 429102371 8月 1 00:00 access_20200731.log
-rw-r--r-- 1 nobody root 336208264 8月 2 00:00 access_20200801.log
-rw-r--r-- 1 nobody root 351997028 8月 3 00:00 access_20200802.log
-rw-r--r-- 1 nobody root 67568623 8月 3 08:54 access.log
以上是关于Nginx:日志配置与日志定时分割备份的主要内容,如果未能解决你的问题,请参考以下文章
nginx--基于crond定时服务+shell脚本实现nginx日志自动清理及备份