Nginx Log File Format and cut Log File
As a Web server, logs are indispensable and need to be viewed frequently. This article introduces how to customize the log format in Nginx and use crontab to schedule tasks to cut log files once a day for convenient management.
In Nginx, the log file is defined by the log_format command. Its syntax is as follows:
Log_format name format
# Name: the name of the log format (called later)
# Format: Set the log format
Nginx has its own default log format, which is as follows:
# Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "'
# '$ Status $ body_bytes_sent "$ http_referer "'
# '"$ Http_user_agent" "$ http_x_forwarded_for "';
It is commented out by default. Let's take a look at the explanation.
$ Remote_addr |
Ip address of the client (if a proxy server exists in the middle, the ip address displayed here is the ip address of the proxy server) |
$ Remote_user |
Used to record the User Name of the remote client (generally "-") |
$ Time_local |
Used to record access time and time zone |
$ Request |
Used to record the request url and Request Method |
$ Status |
Response status code |
$ Body_bytes_sent |
Size of the file body sent to the client |
$ Http_user_agent |
Proxy used by the user (generally in the browser) |
$ Http_x_forwarded_for |
The Client IP address can be recorded, and the client IP address can be recorded through the Proxy Server |
$ Http_referer |
You can record the link from which the user accesses the data. |
Sometimes, even if $ http_x_forwarded_for is enabled, the Client ip address cannot be obtained. For details, refer to this blog post: Nginx logs cannot be obtained and the remote access ip address is resolved.
You can quickly customize a log format by referring to the above content. Just add it to the previous VM.
The last time there was no custom log format and access logs were not enabled on the restricted access status page, write one.
Log_format access' $ remote_addr $ remote_user "$ time_local" $ request'
'$ Status $ http_referer'
'"$ Http_user_agent" $ http_x_forwarded_for ';
The added name is the access log that records the ip address, user, time, corresponding method and page, Status Code, from which link, user proxy, and if there is a proxy in the middle, the user is also obtained from the proxy ip, then open the access log of the status page and give it to a file path (this file may not exist, and the directory must exist), and specify the name we just set after the path, also, reference the access logs on the normal page to the custom log format. (If the name is not set, the default format is used)
Check whether the configuration is correct after the definition is complete.
Nginx-t
Nginx provides a warning message, warning that log_format may be used under the http level in/usr/local/nginx. conf. To fix this warning, follow these steps.
Put our custom information in the http in the nginx. conf configuration file (you must delete the custom log_format in the configuration file of the VM ):
Check again to work properly
Delete or rename the original access. log file, and re-read the configuration file.
Cd/web/vhost/test1/logs/
Mv test1.access. log access. log. bak
Nginx-s reload # repeat the configuration file
Then visit to see if the log file is the same as our custom content:
The access logs of the status page record the ip address, user name, time, request method, accessed page, and page status code (because "-" is not added during definition, the effect is not very good), the browser used by the client, and the proxy server (the local access does not have a proxy server, so it is -)
The custom log ends. The next step is to cut the log file and save a log file of the current day every day as follows:
First, rename the original log file.
Mv test1.access. log test1.2014-12-29.log
Nginx-s reload
This allows you to cut log files. If you want to save it automatically every day, you need to manage it Based On crontab. Write a script.
#! /Bin/bash
#
#
Vhost = test1 # define the Directory Name of the VM
Log_path =/web/vhost/logs # specify the location where the log file is to be saved
Access_log_path =/web/vhost/$ {vhost}/logs # specify the path of the access log
Mkdir-p $ {log_path}/$ (date-d "yesterday" + "% y")/$ (date-d "yesterday" + % m) // # create date directory, date-d "yesterday" + "% y" Get the month after the year
Mv $ {access_log_path}/$ {vhost }. access. log $ {log_path}/$ (date-d "yesterday" + "% y")/$ (date-d "yesterday" + % m) /$ (date-d "yesterday" + % d ). $ {vhost }. access. log # Here, move the access log file to the backup directory, such as/web/vhost/logs/14/12/28. test1.access. log
Nginx-s reload # Here I use the -- sbin-path =/usr/sbin/nginx parameter during nginx compilation, so I can directly use nginx if this parameter is not used make sure to add the sbin directory under your nginx installation directory to the system environment variables.
Then, place the script file in the nginx configuration directory to add a scheduled task.
Mkdir/usr/local/nginx/bin & mv logs. sh/usr/local/nginx/bin/logs. sh
Crontab-e
00 00 ***/bin/bash/usr/local/nginx/bin/logs. sh
In this way, the backup file will be automatically created at every night. If you want to back up the error log every day, modify the file according to the above content.
-------------------------------------- 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
-------------------------------------- Split line --------------------------------------
Nginx details: click here
Nginx: click here
This article permanently updates the link address: