Twelfth three-time class (April 25)
12.10 Nginx access log
12.11 Nginx log cutting
12.12 Static files do not record logs and expire time
Nginx access log
Log format
vim /usr/local/nginx/conf/nginx.conf //Search log_format
In addition to defining the log format in the main configuration file nginx.conf, you also need to add
access_log /tmp/1.log combined_realip to the virtual host configuration file ;
The combined_realip here is the log format name defined in nginx.conf
-t && -s reload
curl -x127.0.0.1:80 test.com -I
cat /tmp/1.log
[root@izbp1iei7rfo61naj23hlez vhost]# cat /tmp/1.log
127.0.0.1 - [25/Apr/2018:22:03:28 +0800] test3.com "/index.html" 301 "-" "curl/7.29.0"
127.0.0.1 - [25/Apr/2018:22:03:50 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
Nginx log cutting
The custom shell script
vim /usr/local/sbin/nginx_log_rotate.sh// writes the following content
#! /bin/bash
#假设nginx的日志存放路径为/tmp/
d=`date -d "-1 day" +%Y%m%d`
logdir="/tmp/"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`
perform tasks
[root@izbp1iei7rfo61naj23hlez vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
- d=20180424
- logdir=/tmp/
- nginx_pid=/usr/local/nginx/logs/nginx.pid
- cd /tmp/
++ ls 1.log- for log in '
ls *.log
'- mv 1.log 1.log-20180424
++ cat /usr/local/nginx/logs/nginx.pid- /bin/kill -HUP 13116
task schedule crontab -e
0 0 * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
Static files do not log and expire
vi /usr/local/nginx/conf/vhost/test.com.conf
is configured as follows
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
Here are some times defined and some logs recorded