# What is the best way to rotate NGINX log files?

**URL:** <https://www.ruby-forum.com/t/what-is-the-best-way-to-rotate-nginx-log-files/122235>\
**Category:** NGINX\
**Created:** [December 5, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-rotate-nginx-log-files/122235 "2007-12-05T19:49:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mansoor\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f14d63/32.png) [@Mansoor\_P](https://www.ruby-forum.com/u/Mansoor_P)\
**Post date:** [December 5, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-rotate-nginx-log-files/122235/1 "2007-12-05T19:49:47Z")

</div>

Hello Igor,

I have a question. What’s the best way to rotate the NGINX log file ? We  
run NGINX as a Mail Proxy, and the log file grows quite large because we  
don’t restart it very frequently.

Are there any alternatives to simply killing the NGINX process, zipping  
up the error log, and then restarting NGINX ?

Regards,  
Mansoor

---

<div class="post-metadata">

**Author:** ![Mansoor\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f14d63/32.png) [@Mansoor\_P](https://www.ruby-forum.com/u/Mansoor_P)\
**Post date:** [December 5, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-rotate-nginx-log-files/122235/2 "2007-12-05T19:59:44Z")

</div>

There is no need to kill nginx. Use logrotate or something.

---

<div class="post-metadata">

**Author:** ![Mansoor\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f14d63/32.png) [@Mansoor\_P](https://www.ruby-forum.com/u/Mansoor_P)\
**Post date:** [December 5, 2007, 8:03pm UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-rotate-nginx-log-files/122235/3 "2007-12-05T20:03:52Z")

</div>

On Wed, Dec 05, 2007 at 10:41:30AM -0800, Mansoor P. wrote:

> I have a question. What’s the best way to rotate the NGINX log file ? We run NGINX as a Mail Proxy, and the log file grows quite large because we don’t restart it very frequently.
> 
> Are there any alternatives to simply killing the NGINX process, zipping up the error log, and then restarting NGINX ?

Of course!

mv error.log error.log.0  
kill -USR1 `cat master.nginx.pid`  
wait 1 second and do anything with log

---

<div class="post-metadata">

**Author:** ![Mansoor\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f14d63/32.png) [@Mansoor\_P](https://www.ruby-forum.com/u/Mansoor_P)\
**Post date:** [December 8, 2007, 9:49am UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-rotate-nginx-log-files/122235/4 "2007-12-08T09:49:58Z")

</div>

Thank you, Igor !

----- Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
| On Wed, Dec 05, 2007 at 10:41:30AM -0800, Mansoor P. wrote:  
|  
| \> I have a question. What’s the best way to rotate the NGINX log file ? We run NGINX as a Mail Proxy, and the log file grows quite large because we don’t restart it very frequently.  
| \>  
| \> Are there any alternatives to simply killing the NGINX process, zipping up the error log, and then restarting NGINX ?  
|  
| Of course!  
|  
| mv error.log error.log.0  
| kill -USR1 `cat master.nginx.pid`  
| wait 1 second and do anything with log  
|  
|  
| –  
| Igor S.  
| [Igor Sysoev](http://sysoev.ru/en/)  
|  
|

---

<div class="post-metadata">

**Author:** ![Mansoor\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f14d63/32.png) [@Mansoor\_P](https://www.ruby-forum.com/u/Mansoor_P)\
**Post date:** [December 8, 2007, 2:52pm UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-rotate-nginx-log-files/122235/5 "2007-12-08T14:52:30Z")

</div>

Note that those steps can be handled by most syslog rotating programs by  
specifying either the pid file (maybe with separate config file and  
crontab entry), or by adding the kill line as a custom command in the  
main rotation config file. 🙂

For instance, on my OpenBSD systems, in newsyslog.conf after the ‘flags’  
fields is the field for pif file, so…

# logfile owner:group mode count size when flags

/…/access.log 644 9 512 \* Z /…/nginx.pid
