12.10 Nginx Access Log
- Access log test results:
Curl-x127.0.0.1:80 Test.com-i
Curl-x127.0.0.1:80 Test2.com-i
Cat/tmp/1.log
12.11 Nginx Log Cutting
- As stated earlier, in order to prevent the log from taking up too much storage space, we need to cut the logs and clean them regularly. Since Nginx does not have a cutting tool, learn to use shell scripting to implement log cutting.
Custom shell Scripts
Vim/usr/local/sbin/nginx_log_rotate.sh #写入如下内容
#! /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 '
- execute the log cut script :
Sh-x/usr/local/sbin/nginx_log_rotate.sh #加-X can see the script execution process
- Set up a log cutting task schedule
Crontab-e
Add the following line:
0 0 * * */bin/bash/usr/local/sbin/nginx_log_rotate.sh
- example of periodic cleanup log files (delete log files for more than 30 days):
find/tmp/-name *.log-*-tpye f-mtime +30 | Xargs RM
12.12 static files do not log logs and expire time
- To set how static files do not log and expire time:
- Edit a virtual host configuration file
Vim/usr/local/nginx/conf/vhost/test.com.conf
- Configured as follows
Location ~. \. (gif|jpg|jpeg|png|bmp|swf) $
{
Expires 7d;
Access_log off;
}
Location ~. \. (JS|CSS) $
{
Expires 12h;
Access_log off;
}
- /usr/local/nginx/sbin/nginx-t
- /usr/local/nginx/sbin/nginx-s Reload
- Test results:
Cd/data/wwwroot/test.com
Vim 1.gif
Vim 2.css
Curl-x127.0.0.1:80 Test.com/1.gif
Curl-x127.0.0.1:80 Test.com/2.css
Curl-x127.0.0.1:80 test.com/index.html
Curl-x127.0.0.1:80 TEST.COM/2.CSSASDFA
Curl-x127.0.0.1:80 Test.com/1.gif-i
Cat/tmp/1.log
2018-3-14 Linux Learning Notes