可以自己写脚本切割,并指定crontab定时运行。
    如按天切割,定期0:00运行一个脚本,脚本步骤:
    1)先mv日志文件到新文件名
    2)nginx -s reload

    或者使用logrotate工具:

    1. cat /etc/logrotate.d/nginx
    2. /var/log/nginx/*.log {
    3. daily
    4. dateext
    5. missingok
    6. rotate 30
    7. compress
    8. delaycompress
    9. notifempty
    10. create 644 root root
    11. sharedscripts
    12. postrotate
    13. [ -f /usr/local/nginx/nginx.pid ] && kill -USR1 `cat /usr/local/nginx/nginx.pid`
    14. endscript
    15. }

    参数解释,按上面的顺序:
    daily #每天执行
    dateext #后缀为日期
    missingok #没有可以不分割
    rotate 30 #30天循环
    compress #压缩
    delaycompress #当天不压缩
    notifempty #空不分割
    create 644 root root #分割后文件的权限
    sharedscripts # 指明脚本是共享的,多个log只会执行一次
    postrotate #分割后执行:重启nginx
    [ -f /usr/local/nginx/nginx.pid ] && kill -USR1 cat /usr/local/nginx/nginx.pid
    endscript #postroate的结束符

    参考:
    https://www.yuque.com/docs/share/a5e929bf-f3ab-4650-bd5d-1ab41a7cb1d9?# 《日志分割logrotate》

    kill -USR1 : USR1信号将导致以下步骤的发生:停止接受新的连接,等待当前连接停止,重新载入配置文件,重新打开日志文件,重启服务器,从而实现相对平滑的不关机的更改
    引申:
    kill -HUP pid 或者 killall -HUP pName:
    其中pid是进程标识,pName是进程的名称
    如果想要更改配置而不需停止并重新启动服务,可以使用上面两个命令。在对配置文件作必要的更改后,发出该命令以动态更新服务配置。