keep the nginx logs of the last 30 days [closed]
Asked Answered
D

3

19

I want to keep the nginx logs of the lsat 30 days. The default configuration is 15 days, as the image shows. enter image description here

I would like to keep the last 30 days instead.

Here are the looging settings of nginx:

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

But it doesn't say anything about "how oft" it should be taken.

I'm not a nginx expert at all, so I don't know how/where I can change that configuration.

Maybe someone there needed to do the same and want to help me.

Dreamy answered 25/5, 2017 at 12:1 Comment(0)
P
27

To change this behavior, you'll have to change the logrotate file of nginx. This file is probably located in /etc/logrotate.d. For achieving what you're trying to do, put the directives weekly and rotate 30 inside the file corresponding to nginx. After that, use the following command to ensure the changes take effect:

  • logrotate /etc/logrotate.d/nginx-config-file
Paradise answered 25/5, 2017 at 12:13 Comment(2)
@Paradise wouldn't that config store log files for 30 weeks instead of the last 30 days? (rotate file each week, keep 30 files)Joanne
that sounds right, and you should simply need to swap weekly for dailyKochi
D
13

You can set up logrotate for nginx, in this way you can maintain logs for 30 days or more as per your requirements !

/etc/logrotate.d/nginx


/var/log/nginx/access_log {
rotate 7
size 5k
dateext
dateformat -%Y-%m-%d
missingok
compress
sharedscripts
postrotate
    test -r /var/run/nginx.pid && kill -USR1 `cat /var/run/nginx.pid`
endscript
}

change value of #rotate accordingly ! 30,40 etc etc ...

Dogvane answered 25/5, 2017 at 12:18 Comment(0)
L
4

Logrotate is a utility not specific to nginx that is installed by default on Ubuntu/Debian designed to ease management of log files. You can find config info and background at the manpages:

man logrotate

On my Debian server, the config for me is stored in /etc/logrotate.d/nginx. The manpages give examples of config files with intuitive names. For your case you should have the lines

daily
rotate 30

Save the result with

sudo logrotate /etc/logrotate.d/nginx

For a more in depth tutorial: https://www.digitalocean.com/community/tutorials/how-to-manage-logfiles-with-logrotate-on-ubuntu-16-04

Lumisterol answered 4/5, 2022 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.