Nginx access logs and deletes the logging configuration method before the specified number of days _nginx

Source: Internet
Author: User
Tags time zones mkdir time 0

Description

Operating system: CentOS

Site 1:bbs.jb51.net

Site 2:sns.jb51.net

Nginx Installation path:/usr/local/nginx

Nginx configuration file path:/usr/local/nginx/conf/nginx.conf

Site 1 configuration file path:/usr/local/nginx/conf/vhost/bbs.jb51.net.conf

Site 2 configuration file path:/usr/local/nginx/conf/vhost/sns.jb51.net.conf

Objective:

1, the site 1 and site 2 of the Nginx access log saved by day, log path is:

Site 1:/usr/local/nginx/logs/nginx_logs/bbs_logs

Site 2:/usr/local/nginx/logs/nginx_logs/sns_logs

2, only 30 days to keep the log records

Specific actions:

1. Create Log Store path

Copy Code code as follows:

Mkdir-p/usr/local/nginx/logs/nginx_logs/bbs_logs
Mkdir-p/usr/local/nginx/logs/nginx_logs/sns_logs

2, set nginx logging format

Edit vi/usr/local/nginx/conf/nginx.conf

Copy Code code 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 ';

Cancel the comment in front of the logging format and add a row at the end, as follows:

Copy Code code 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"
' $http _host $upstream _status $upstream _addr $request _time $upstream _response_time '; #新添加的一行

: wq! #保存退出

3, set up the virtual host configuration file

Edit Vi/usr/local/nginx/conf/vhost/bbs.jb51.net.conf #, in bbs.jb51.net; add below this line

Copy Code code as follows:

Access_log Logs/bbs_access.log Main; #注意这里的main要和上一步中的main一致

: wq! #保存退出

Edit vi/usr/local/nginx/conf/vhost/sns.jb51.net.conf, in sns.jb51.net; add below this line

Copy Code code as follows:

Access_log Logs/sns_access.log Main; #注意这里的main要和上一步中的main一致

Copy Code code as follows:

: wq! #保存退出
Service Nginx Reload #重新加载nginx配置文件

4. Create Nginx Log Cutting script

Edit vi/home/crontab/cut_nginx_log.sh

Copy Code code as follows:

#!/bin/sh
Logs_path= "/usr/local/nginx/logs/"
Days=30
MV ${logs_path}bbs_access.log ${logs_path}nginx_logs/bbs_logs/bbs_access_$ (date-d "Yesterday" + "%Y%m%d"). Log
MV ${logs_path}sns_access.log ${logs_path}nginx_logs/sns_logs/sns_access_$ (date-d "Yesterday" + "%Y%m%d"). Log
KILL-USR1 ' Cat/usr/local/nginx/logs/nginx.pid '
Find ${logs_path}nginx_logs/bbs_logs/-name "bbs_access_*"-type f-mtime + $DAYS-exec rm {} \;
Find ${logs_path}nginx_logs/sns_logs/-name "sns_access_*"-type f-mtime + $DAYS-exec rm {} \;

Copy Code code as follows:

: wq! #保存退出
chmod +x/home/crontab/cut_nginx_log.sh #添加脚本执行权限

5, add Task plan, modify/etc/crontab

Vi/etc/crontab added on the last line

Copy Code code as follows:

0 0 * * * root/home/crontab/cut_nginx_log.sh #表示每天凌晨执行

: wq! #保存退出

6, restart Crond to make the settings effective

Copy Code code as follows:

/etc/rc.d/init.d/crond Restart #yum install-y Vixie-cron installation Scheduled Tasks, some systems may not have pre-installed
Chkconfig Crond on #设为开机启动
Service Crond Start #启动

Every day in/usr/local/nginx/logs/nginx_logs/bbs_logs and/usr/local/nginx/logs/nginx_logs/sns_logs.

Log files similar to Bbs_access_20140126.log and Bbs_access_20140126.log are generated in the directory

And only keep the log records for the last 30 days

At this point, Linux under the scheduled cutting Nginx access log and delete the specified number of days before the completion of the log record.

Note: If the following error occurred during the execution of the script

Copy Code code as follows:

Nginx: [ERROR] Open () "/usr/local/nginx/logs/nginx.pid" failed

Solution:

Copy Code code as follows:

/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf
#使用nginx-C parameter specifies the location of the nginx.conf file

Extended reading: Nginx Access log parameter description

Copy Code code as follows:

192.168.21.1--[27/jan/2014:11:28:53 +0800] "get/2.php http/1.1"-"" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/33.0.1707.0 safari/537.36 ""-"192.168.21.128 200 127.0.0.1:9000 0.119 0.119

$remote _addr: Client Address 192.168.21.1

$remote _user: Client User-

$time _local: Times and time zones 27/jan/2014:11:28:53 +0800

$request: Requested URL path and HTTP protocol get/2.php http/1.1

$status: HTTP status 200

$body _bytes_sent: Sent to client page size 133

$http _referer: page Jump Source-

$http _user_agent: User access to Terminal mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/33.0.1707.0 safari/537.36

$http _x_forwarded_for:http Agent, the request-side real IP-

$http _host: User-entered URL (IP or domain name) address in the browser 192.168.21.128

$upstream _status:upstream State 200

$upstream _addr: Back-end upstream address and Port 127.0.0.1:9000

$request _time: Page access Total time 0.119

$upstream _response_time: Upstream response time in page access 0.119

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.