Nginx log File Cutting

Source: Internet
Author: User

Nginx is a very lightweight Web server, small size, high performance, fast, and many other advantages. But shortcomings are also shortcomings, such as the resulting access log file has been one, will not be automatically cut, if the traffic is very large, will result in a very large log file size, not easy to manage. Of course, we do not want to see such a large access log file, it is necessary to manually cut this file.

Shell scripting on the Linux platform is rich, the use of Shell script plus crontab command can be very convenient to cut, but on the Windows platform is a bit more trouble, just a long time, it is here to record a tidy.

Log file Cutting requirements

Since Nginx logs are written in one file, we need to save the previous day's log as another file at 0 o ' Access.log, where we save the Nginx file in the logs directory as Access_[yyyy-mm-dd].log. In fact, logs directory also has a error.log error log file, this file also need to cut one every day, here said Access.log, Error.log cutting method similar.

Linux Platform Cutting

To cut on a Linux platform, use the date command to get yesterday's date, send a signal to the Nginx process to reopen the log file using the Kill command, and Crontab set the execution task cycle.

First create a Shell script, as follows:

Shell Code
    1. #!/bin/bash
    2. # # 0 Points Execute the script
    3. # # The directory where Nginx log files are located
    4. Logs_path=/usr/local/nginx/logs
    5. # # Get yesterday's YYYY-MM-DD
    6. yesterday=$ (date-d "Yesterday" +%y-%m-%d)
    7. # # Move Files
    8. MV ${logs_path}/access.log ${logs_path}/access_${yesterday}.log
    9. # # sends the USR1 signal to the Nginx main process. USR1 signal is to reopen log file
    10. KILL-USR1 $ (cat/usr/local/nginx/nginx.pid)
#!/bin/bash## 0 points to execute the script # # Nginx log files in the directory logs_path=/usr/local/nginx/logs## get yesterday's yyyy-mm-ddyesterday=$ (date-d " Yesterday "+%y-%m-%d) # # Move file mv ${logs_path}/access.log ${logs_path}/access_${yesterday}.log## send USR1 signal to Nginx main process. USR1 signal is re-opened log file KILL-USR1 $ (cat/usr/local/nginx/nginx.pid)

The last line in the above script must send the USR1 signal to the Nginx process to reopen the log file, and if not, Nginx will continue to write the log information to the Access_[yyyy-mm-dd].log file, which is obviously incorrect.

After the script is completed, it is stored in the Nginx installation directory of the Sbin, named Cut-log.sh, and then using CRONTAB-E to add a scheduled task, in which to increase the execution of this script:

Shell Code
    1. 0 0 * * */bin/bash/usr/local/nginx/sbin/cut-log.sh
0 0 * * */bin/bash/usr/local/nginx/sbin/cut-log.sh

To here Linux under the cutting Nginx log is completed, you can set the crontab is closer to the current time to test, otherwise at 0 points out the problem is not good.

Windows Platform Cutting

It's a bit of a hassle to do this on the Windows platform. There are no native commands in Windows to get yesterday's date, scheduled task settings in Windows I don't feel like Linux crontab is easy to use, and there are batch commands and no Shell scripts that are powerful. In short, to solve these problems.

Sina Blog has an article Nginx for Windows log cutting, but this article has two shortcomings: The post-cut log file is not named after yesterday's date; the Nginx service needs to be stopped. In order to cut logs to stop the service, I think it is not worth, if the traffic is small, the problem is not big, but the large number of visits, this practice is very undesirable. To compensate for these shortcomings, we have made improvements to this batch file.

To use the Linux Date command on the Windows platform to get yesterday's date, we can go to sourceforge to download the Unxutils tool. Unxutils is a very powerful toolset, porting most of the Linux commands to the Windows platform, such as LS, grep, WC, etc. 120 commands, of course, including the date tool we need. Unzip the tool into a directory, assuming the D:\common-path\UnxUtils directory, add the D:\common-path\UnxUtils\usr\local\wbin of those tools to the system's environment variable path, Can be added to the last. Because there is a date built-in command in the Windows platform, you need to rename the Unxutils Date.exe to something else, such as Udate.exe instead. Open the console with CMD and enter:

D:\>udate-d "Yesterday" +%y-%m-%d2010-05-31d:\>_

If we can properly output yesterday's date, then we'll take care of that.

Next we need to write a batch file, assuming that our Nginx is placed in the d:\httpServer\nginx-0.7.64 directory, we will build a Cut-log.bat file in this directory:

Batch Code
  1. @echo off
  2. REM Get yesterday's date, deposit yesterday variable, udate parameter% need to change to percent to escape
  3. for/f%%a in (' Udate-d "Yesterday" +%%y-%%m-%%d ') do set yesterday=%%a
  4. REM Set the drive letter where Nginx is located
  5. Set Nginx_driver=d:
  6. REM Set Nginx's home directory
  7. Set nginx_path=%nginx_driver%\green\httpserver\nginx-0.7.64
  8. REM Set Nginx log directory
  9. Set Log_path=%nginx_path%\logs
  10. REM Moving files
  11. Move%log_path%\access.log%log_path%\access_%yesterday%.log
  12. REM Switch to the drive letter where Nginx is located
  13. %nginx_driver%
  14. REM enters Nginx's home directory
  15. CD%nginx_path%
  16. REM sends reopen signals to Nginx to reopen log files, which is consistent with KILL-USR1 in the Linux platform
  17. Nginx-s Reopen
  18. echo on
@echo Offrem  Get yesterday's date, deposit the yesterday variable, the% in the udate parameter needs to be changed to percent to escape for/f%%a in (' udate-d ' Yesterday ' +%%y-%%m-%%d ') do s ET Yesterday=%%arem set nginx in the drive letter set Nginx_driver=d:rem set Nginx home directory set Nginx_path=%nginx_driver%\green\httpserver\ Nginx-0.7.64rem set Nginx log directory set log_path=%nginx_path%\logsrem move file%log_path%\access.log%log_path%\access_%  Yesterday%.logrem switch to Nginx's drive letter%nginx_driver%rem into the Nginx home directory CD%nginx_path%rem send reopen signal to Nginx to reopen the log file, function and Linux KILL-USR1 consistent nginx-s in the platform Reopenecho on

After this batch is written, it is added to the scheduled task in Windows, which is set to execute 0 o'clock every day. Note that in the execution of the nginx-s reopen command, the current directory must be in the Nginx home directory, otherwise the log file will not be found in which directory (the Nginx default is estimated to use a relative path to find), which is the batch processing needs to enter the drive letter and Nginx home directory reason, Because the task is scheduled to execute, it is not in the Nginx home directory.

Conclusion

Here is the main introduction to the Linux platform and the Windows platform on the cutting Nginx log file method. Linux can be done directly with some built-in commands, and the Unxutils tool needs to be loaded in Windows, but this toolset is very useful, such as using the tail command, which makes it easy to use tail-f in the console to view logs in real time in Windows. The output of the file.

More references
    • Need to know more about using the KILL command in Linux to send other signals to the nginx process, and the meaning of the signal can be referenced in the Nginx Chinese wiki operation and control nginx– command line parameters and signals of this Chinese document.
    • The Windows platform uses the NGINX-S command to send various signals to the NGINX main process, and the meaning of the signal can be found at the end of the installation Nginx article.

Nginx log File Cutting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.