참고 : https://sronsemiro.tistory.com/31
ChatGPT
In Nginx, log files are rotated using the nginx -s reopen command. This command causes Nginx to close and reopen its log files. When used in conjunction with a log rotation tool such as logrotate, the log files can be rotated on a regular schedule.
To set up log rotation for Nginx logs, you can use the following steps:
Install the logrotate tool on your system if it is not already installed.
Create a configuration file for logrotate in /etc/logrotate.d/nginx with the following content:
/var/log/nginx/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
This configuration will rotate the Nginx logs daily, keep 7 days worth of logs, compress rotated logs, and send a signal to Nginx to close and reopen its log files after the rotation.
Run the logrotate command to test the configuration and rotate the logs.
sudo logrotate -v /etc/logrotate.d/nginx
If the configuration and rotation are successful, you can set up a cron job to run the logrotate command on a regular schedule.
For example, to run logrotate daily, you can add the following line to the /etc/crontab file:
0 0 * * * root /usr/sbin/logrotate /etc/logrotate.d/nginx
This will run logrotate daily at midnight and rotate the Nginx logs according to the configuration in /etc/logrotate.d/nginx.