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天