Nginx access logs, Nginx log cutting, static files do not record logs and expiration time

 Nginx access log:

vim /usr/local/nginx/conf/nginx.conf //Search log_format = The log format can be found in the configuration file

Define access log

Defining the log needs to be defined in the virtual host. If the name of the log format is modified in nginx, then defining the log file in the virtual host needs to refer to the name in the nginx configuration file.

vim /usr/local/nginx/conf/vhost/test.com.log=Define virtual host Japanese file

After defining -t && -s reload   

[root@aming-01 vhost]# /usr/local/nginx/sbin/nginx -t      = 检测配置文件语法是否错误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@aming-01 vhost]# /usr/local/nginx/sbin/nginx -s reload    = 重新加载配置文件

Test configuration: status code 200 means ok

cat /tmp/test.com.log = View the information in the log

 

 

Nginx log cutting:

nginx log cutting needs to use the tools that come with the system, or write a cutting script

vim /usr/local/sbin/nginx_log_rotate.sh//Write the following content = configure the log cutting script required by nginx

#! /bin/bash
## 假设nginx的日志存放路径为/data/logs/
d=`date -d "-1 day" +%Y%m%d`      = 定义时间(昨天的日期)
logdir="/data/logs"       =定义日志存放路径
nginx_pid="/usr/local/nginx/logs/nginx.pid"     
cd $logdir              =  进入到日志目录下
for log in `ls *.log`          = for循环匹配
do
    mv $log $log-$d     =   更改所有配置文件的名字
done
/bin/kill -HUP `cat $nginx_pid`

After configuring the cutting script, you can execute the test script sh -x plus the script path -x = can display the script execution process

[root@aming-01 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180425
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.com.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180425
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1089

After writing the script, you need to add a task plan:

crontab -e = task scheduler

0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh    = 表示每天凌晨0点执行脚本

 

Static files do not log and expire:

The same is configured in the virtual host

vim /usr/local/nginx/conf/vhost/test.com.conf = test virtual host configuration file

After configuration, you also need -t && -s reload to check the configuration file syntax for errors and reload the configuration file

[root@aming-01 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@aming-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

Test: curl -x127.0.0.1:80 test.com/1.gif/2.gs

cat /tmp/test.con.log

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324923062&siteId=291194637