nginx如何做日志切割 
时间: 2023-04-04 07:01:30 浏览: 51
nginx可以通过配置日志切割来实现日志的自动切割。具体操作是在nginx.conf配置文件中添加以下内容:
1. 在http段中添加:
```
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log access;
```
2. 在server段中添加:
```
access_log /var/log/nginx/access.log access;
error_log /var/log/nginx/error.log;
```
3. 在logrotate.d目录下创建一个nginx文件,内容如下:
```
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 064 nginx adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
```
这样配置后,nginx会在每天的凌晨自动将access.log文件切割成access.log.1、access.log.2.gz、access.log.3.gz等多个文件,以便于管理和查看。
相关推荐









