Note _ Nginx log cutting and _ nginx log Cutting

Source: Internet
Author: User
Tags dateformat

Note _ Nginx log cutting and _ nginx log Cutting

Grep generates the nginx main configuration file

Egrep-v "# | ^ $" nginx. conf. default> nginx. conf

 

Log format

[Root @ vfast conf] # sed-n'21, 23 s/# // gp 'nginx. conf. default
Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "'
'$ Status $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" "$ http_x_forwarded_for "';

The specific configuration file is as follows:

[Root @ vfast conf] #Cat nginx. conf
Worker_processes 1;
Events {
Worker_connections 1024;
}
Http {
Include mime. types;
Default_type application/octet-stream;
Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request"' configure the log format
'$ Status $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" "$ http_x_forwarded_for "';

Sendfile on;
Keepalive_timeout 65;
Server {
Listen 80;
Server_nameWww. blog. conf;
Location /{
RootHtml/www;
Index index.html index.htm;
}
Access_log logs/access_www.log main; Take www.blog.com as an example to generate related log files
Error_page 500 502 503 x.html;
Location =/50x.html {
Root html;
}
}
}

Create root directory

[Root @ vfast html] # mkdir www

[Root @ vfast www] # vim index.html
[Root @ vfast www] # cat index.html
Http://www.blog.com

Add hosts resolution

[Root @ vfast www] # vim/etc/hosts
[Root @ vfast www] # cat/etc/hosts
127.0.0.1 localhost. localdomain localhost4 localhost4.localdomain4
: 1 localhost. localdomain localhost6 localhost6.localdomain6
192.168.31.234 www.blog.com

 

Reload nginx

[Root @ vfast www] #/application/nginx/sbin/nginx-s reload

 

Test

[Root @ vfast www] # curl www.blog.com
Http://www.blog.com

 

Nginx generates all access logs to access_www.log by default, and performs log polling and cutting to facilitate log analysis and processing. The daily splitting method is used here.

The cut script is as follows:Cut_nginx_log.sh

#! /Bin/bash

Dateformat = 'date + % Y % m % d'
Basedir = "/application/nginx"
Nginxlogdir = "$ Basedir/logs"
Logname = "access_www"
[-D $ Nginxlogdir] & cd $ Nginxlogdir | exit 1
[-F $ {Logname}. log] | exit 1
/Bin/mv $ {Logname}. log $ {Logname }_$ {Dateformat}. Log
$ Basedir/sbin/nginx-s reload

 

Scheduled task execution: Execute cut_nginx_log.sh to cut logs at every day

 

00 00 ***/bin/sh/server/script/cut_nginx_logs.sh>/dev/null 2> & 1

 

 

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.