nginx訪問日誌:css
vim /usr/local/nginx/conf/nginx.conf #搜索log_format 該字段定義日誌格式,默認以下:nginx
#combined_realip日誌格式的名字,可隨意定義;vim
需在虛擬主機配置文件裏面中添加配置。bash
1.編輯虛擬主機配置文件,curl
vim /usr/local/nginx/conf/vhost/test.com.conf ## 在server{}中增長「access_log /tmp/test.com.log combined_realip;」 #日誌路徑和名稱爲/tmp/test.com.log;combined_realip爲nginx.conf中日誌格式的名字。測試
2.檢查並從新加載配置文件:網站
/usr/local/nginx/sbin/nginx -t ;/usr/local/nginx/sbin/nginx -s reloadurl
3.使用curl進行訪問測試:spa
4.查看對應日誌目錄的日誌內容:日誌
nginx日誌切割:
編寫日誌切割腳本:
vim /usr/local/sbin/nginx_log_rotate.sh
#! /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` do mv $log $log-$d done /bin/kill -HUP `cat $nginx_pid`
靜態文件不記錄日誌和過時時間
1.編輯虛擬主機配置文件:
vim /usr/local/nginx/conf/vhost/test.com.conf # 在server{}中增長:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #匹配以 .xxx結尾的,xxx爲()裏字符串 { expires 7d; #過時時間爲7天 access_log off; #訪問日誌關閉 } location ~ .*\.(js|css)$ { expires 12h; access_log off; }
2.檢查並從新加載配置文件:
/usr/local/nginx/sbin/nginx -t ;/usr/local/nginx/sbin/nginx -s reload
3.建立測試文件:
cd /data/wwroot/test.com;echo "123134" > 1.gif ;echo "1244536" > 2.js #在網站根目錄建立測試gif和js文件
4.訪問測試並查看日誌:
curl -x127.0.0.1:80 test.com/1.gif
curl -x127.0.0.1:80 test.com/2.js
cat /tmp/test.com.log #若配置正確,此處日誌並不會顯示1.gif和2.js的訪問記錄
5.查看過去時間字段是否生效:max-age=43200 單位爲妙,即12h,和上面配置對應