12.10 Nginx访问日志css
12.11 Nginx日志切割html
12.12 静态文件不记录日志和过时时间nginx
日志格式shell
vim /usr/local/nginx/conf/nginx.conf //搜索log_format
虚拟主机启用日志apache
access_log /tmp/ccc.log combined_realip;
-t && -s reload curl -x127.0.0.1:80 ccc.com -I cat /tmp/ccc.log //vim
vim /usr/local/sbin/nginx_log_rotate.sh//写入以下内容 ··· #! /bin/bash # 定义变量d为昨天日期 d=`date -d "-1 day" +%Y%m%d` # 定义变量logdir为日志路径,若是不知道能够看虚拟主机配置 logdir="/tmp/" #定义nginx_pid为日志进程文件 nginx_pid="/usr/local/nginx/logs/nginx.pid" cd $logdir # i in commed 将i循环赋值为命令的每一个输出,进行do的操做 for i in `ls *.log` do mv $i $i-$d done # 文件更名后须要中断日志pid,不然新日志没法生成 /bin/kill -HUP `cat $nginx_pid`
手动执行(-x显示执行过程)浏览器
[root@axiang-02 ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh
任务计划(天天0点执行)bash
crontab -e ... 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
核心配置服务器
location ~ .*(gif|jpg|jpeg|png|bmp|swf)$ //正则精准匹配 { expires 7d; //过时时间 access_log off; //不记录日志 } location ~ .*\.(js|css)$ { expires 12h; access_log off; }
测试curl
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/1.gif asfoawnfnasxojfan [root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/2.js soawejifna [root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/index.html This is ccc.com [root@axiang-02 ccc.com]# cat /tmp/ccc.log //没重启nginx会继续生成静态文件日志 127.0.0.1 - [06/Aug/2017:09:41:47 +0800] ccc.com "/1.gif" 200 "-" "curl/7.29.0" 127.0.0.1 - [06/Aug/2017:09:41:57 +0800] ccc.com "/2.js" 200 "-" "curl/7.29.0" 127.0.0.1 - [06/Aug/2017:09:42:16 +0800] ccc.com "/index.html" 200 "-" "curl/7.29.0" [root@axiang-02 ccc.com]# /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@axiang-02 ccc.com]# /usr/local/nginx/sbin/nginx -s reload [root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/index.html This is ccc.com [root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/2.js soawejifna [root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/1.gif asfoawnfnasxojfan [root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/index.html This is ccc.com [root@axiang-02 ccc.com]# !cat cat /tmp/ccc.log 127.0.0.1 - [06/Aug/2017:09:41:47 +0800] ccc.com "/1.gif" 200 "-" "curl/7.29.0" 127.0.0.1 - [06/Aug/2017:09:41:57 +0800] ccc.com "/2.js" 200 "-" "curl/7.29.0" 127.0.0.1 - [06/Aug/2017:09:42:16 +0800] ccc.com "/index.html" 200 "-" "curl/7.29.0" 127.0.0.1 - [06/Aug/2017:09:42:57 +0800] ccc.com "/index.html" 200 "-" "curl/7.29.0" [root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/1.gif -I HTTP/1.1 200 OK ... Cache-Control: max-age=604800 //失效为7天