Manage Nginx logs and use scheduled tasks to cut logs
Ngnix log management and log cutting with scheduled tasks
I. Log Management
Let's take a look at the server segment of the Ngnix configuration file.
Next, let's explain the meaning of the default format.
# Log_format main '$ remote_addr (remote IP)-$ remote_user (remote user) [$ time_local] (access time) "$ request" (request Method )'
# '$ Status (status 302, 404, 401, 403, etc.) $ body_bytes_sent (Request body length, etc.) "$ http_referer" (referer source information )'
# '"$ Http_user_agent (User Agent)" "$ http_x_forwarded_for (original IP address of the forwarded request )"';
Note: (1) http_x_forwarded_for: When the proxy passes through, the proxy adds your original IP address to this header to transmit your original IP address.
(2) You can also define the log format.
Let's take a look at the log record information.
Ngnix allows different logs for different servers. Next we will create
Save and exit, and then re-load the configuration file
Because the log format is not enabled, you can open the configuration file.
Next, reload the configuration file, access the server, and check whether there is a kelly. log file in the log directory. Then, check more kelly. log for any log records.
Ii. Use a scheduled task to complete log cutting and backup
Idea: use shell to write a script, rename the log of yesterday in the date and time format every 00:00:01, and put it in the corresponding directory, use the USR1 information number to control ngnix to generate a new log file.
Next, create a shell script named runlog. sh in the/usr/local/ngnix/data directory.
I will explain this script in detail
I have attached the source code of the shell script to facilitate testing:
#! /Bin/bash
Base_path = '/usr/local/nginx/logs'
Log_path = $ (date-d yesterday + "% Y % m ")
Day = $ (date-d yesterday + "% d ")
Mkdir-p $ base_path/$ log_path
Mv $ base_path/access. log $ base_path/$ log_path/access _ $ day. log
# Echo $ base_path/$ log_path/access _ $ day. log
Kill-USR1 'cat/usr/local/nginx/logs/nginx. pid'
Next, perform a scheduled task to automatically execute this script at the specified time (here I am doing a test, so I will execute this script every minute to make it easy to see the effect)
View the final result
At this point, we have completed the script execution with the scheduled task, and then performed the log cutting backup.
-------------------------------------- Split line --------------------------------------
Deployment of Nginx + MySQL + PHP in CentOS 6.2
Build a WEB server using Nginx
Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5
Performance Tuning for Nginx in CentOS 6.3
Configure Nginx to load the ngx_pagespeed module in CentOS 6.3
Install and configure Nginx + Pcre + php-fpm in CentOS 6.4
Nginx installation and configuration instructions
Nginx log filtering using ngx_log_if does not record specific logs
Nginx details: click here
Nginx: click here
This article permanently updates the link address: