Nginx Log File Cutting

Source: Internet
Author: User

It is accidentally found that access. log is 21 GB big, so it is cut.

Nginx is a lightweight Web server with many advantages such as small size, high performance, and fast speed. However, there are also shortcomings. For example, the access log file generated by the log file is always one and will not be automatically cut. If the access traffic is large, the log file capacity will be very large, it is not easy to manage. Of course, we do not want to see such a huge access log file. We need to manually cut this file.

There are a variety of shell scripts on the Linux platform. It is very convenient to use the shell script and crontab command to cut, but it is troublesome on the Windows platform. It just took a long time, record it here.

Log File cutting requirements

Because nginx logs are written in one file, we need to save the log of the previous day as another file at every day. Here we will place nginx in the access in the logs directory. log is stored as access _ [yyyy-mm-dd]. log File. In fact, there is an error. log file in the logs directory. This file also needs to be cut every day. Here we will talk about access. log. The method for cutting error. log is similar.

Linux platform Cutting

For cutting on Linux, you need to use the date command to obtain the date of yesterday, use the kill command to send the signal to the nginx process to re-open the log file, and crontab to set the task execution cycle.

Create a shell script as follows:

ShellCode
    1. #! /Bin/bash
    2. # Execute the script
    3. # Nginx log file directory
    4. Logs_path =/usr/local/nginx/logs
    5. # Retrieve yesterday's yyyy-mm-dd
    6. Yesterday = $ (date-D "yesterday" + % Y-% m-% d)
    7. # Move a file
    8. MV $ {logs_path}/access. Log $ {logs_path}/Access _ $ {yesterday}. Log
    9. # Send the usr1 signal to the nginx main process. The usr1 signal re-opens the log file
    10. Kill-usr1 $ (CAT/usr/local/nginx. PID)
 
#! /Bin/bash # execute this script at # logs_path =/usr/local/nginx/logs # obtain yesterday's yyyy-mm-ddyesterday = $ (Date-d "yesterday" + % Y-% m-% d) # Move the file MV $ {logs_path}/access. log $ {logs_path}/Access _ $ {yesterday }. log # Send the usr1 signal to the nginx main process. The usr1 signal is to re-open the log file kill-usr1 $ (CAT/usr/local/nginx. PID)

The last line in this script must send the usr1 signal to the nginx process to re-open the log file, nginx will continue to write log information into Access _ [yyyy-mm-dd]. this is obviously incorrect in the log file.

After the script is complete, save it to the sbin of the nginx installation directory, named cut-log.sh, and then use crontab-e to add a scheduled task in which to execute the 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

Here, the nginx log is cut in Linux. You can set crontab to a time closer to the current time for testing. Otherwise, the problem will not be good at zero.

Windows platform Cutting

It is a little troublesome to do this on the Windows platform. In Windows, there are no native commands to get the date of yesterday. In windows, I don't feel like Linux Crontab is easy to use. In addition, batchcompute commands do not have powerful shell scripts. In short, let's solve these problems one by one.

There is an article on Sina BlogArticleNginx for Windows Log cutting, but this article has two shortcomings: the log file after cutting is not named after the date of yesterday; nginx service needs to be stopped. In order to cut the log and stop the service, I think it is not worth it. If the traffic is small, it is not a problem, but the traffic is large, this approach is very undesirable. To make up for these defects, we have improved this batch file.

To use the date command of Linux on Windows platform to obtain the date of yesterday, we can go to SourceForge to download the unxutils tool. Unxutils is a very powerful tool set. It transplanted most Linux commands to the Windows platform, for example, ls, grep, WC, and other 120 commands, of course, this includes the date tool we need. Decompress the tool to a directory. Assume that the tool is in the D: \ common-path \ unxutils directory: \ common-path \ unxutils \ USR \ Local \ wbin is added to the system environment variable path, which can be added to the end. Because the Windows platform has the date built-in command, you need to rename the date.exe of unxutils to another one, for example, udate.exe. Open the console with cmd and enter:

 
D: \> udate-d "yesterday" + % Y-% m-% d2010-05-31D: \> _

If the date of yesterday can be output correctly, we can solve this problem.

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 create a cut-log.bat file in this directory:

Batch code
  1. @ Echo off
  2. Rem obtains the date of yesterday and saves it to the yesterday variable. % in the udate parameter needs to be changed to % for escape.
  3. For/F % A in ('udate-d "yesterday" + % Y-% m-% D') do set yesterday = %
  4. Rem sets the drive letter in which nginx is located
  5. Set nginx_driver = D:
  6. Rem sets the main directory of nginx
  7. Set nginx_path = % nginx_driver % \ green \ httpserver \ nginx-0.7.64
  8. Rem sets the nginx log directory
  9. Set log_path = % nginx_path % \ logs
  10. Rem move files
  11. Move % log_path % \ Access. Log % log_path % \ Access _ % yesterday %. Log
  12. Switch REM to the drive letter of nginx
  13. % Nginx_driver %
  14. Rem enters the main directory of nginx
  15. Cd % nginx_path %
  16. Rem sends a reopen signal to nginx to re-open the log file, which is consistent with the kill-usr1 function in Linux.
  17. Nginx-s reopen
  18. Echo on
 
@ Echo offrem: Get the date of yesterday and save it to the yesterday variable, % in the udate parameter needs to be changed to % to escape for/F % A in ('udate-d "yesterday" + % Y-% m-% D ') do set yesterday = % arem: Set nginx drive letter set nginx_driver = D: rem sets nginx's main directory set nginx_path = % nginx_driver % \ green \ httpserver \ nginx-0.7.64rem sets nginx's log directory set log_path = % nginx_path % \ logsrem move file move % log_path % \ access. log % log_path % \ Access _ % yesterday %. when logrem switches to the drive letter % nginx_driver % rem of nginx, it enters the main directory cd % nginx_path % rem of nginx and sends a reopen signal to nginx to re-open the log file, the function is consistent with that of kill-usr1 on Linux. nginx-s reopenecho on

After writing this batch, add it to the Windows scheduled task and set it to be executed at every day. Note that the current directory must be in the main directory of nginx when running the nginx-s reopen command, otherwise, the directory in which the log file is located will not be found (it is estimated that nginx uses the relative path by default). This is why the drive letter and nginx main directory need to be entered during batch processing, because the task is not in the nginx main directory during execution.

Conclusion

This section describes how to cut nginx log files on Linux and Windows platforms. In Linux, you can directly use some built-in commands. In Windows, you need to load the unxutils tool. However, this tool set is very useful, such as using the tail command, in Windows, we can also conveniently use tail-F in the console to view the output of log files in real time.

More references
    • To learn more, use the kill command in Linux to send other signals to the nginx process, for the significance of this signal, refer to the Chinese document for running and controlling nginx-command line parameters and signals on the nginx Chinese wiki.
    • On the Windows platform, use the nginx-s command to send various signals to the nginx main process. The significance of the signals can be found at the end of the article on nginx installation.

Permalink: http://www.bluek.org/blog/index.php/2010/08/11/178/

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.