Nginx的訪問日志 Nginx的日志格式是在Nginx的主配置文件中(/usr/local/nginx/conf/nginx.conf) [root@shuai-01 vhost]# vim /usr/local/nginx/conf/nginx.conf 1 可以將日志格式名稱改一下,改為shaui Nginx日志字段的含義 在主配置文件中定義日志的格式,在虛擬主機配置文件中定義日志路徑。 打開虛擬主機配置文件 [root@shuai-01 vhost]# vim .conf access_log /tmp/.log shuai; 1 2 3 注意,Nginx配置文件寫完一行要加“;”,不然就是錯誤。 檢查配置文件語法并重新加載配置文件 [root@shuai-01 vhost]# /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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload 1 2 3 4 檢測: [root@shuai-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.2 Date: Mon, 08 Jan 2018 12:41:20 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http:///index.html [root@shuai-01 vhost]# curl -x127.0.0.1:80 test3.com/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.2 Date: Mon, 08 Jan 2018 12:41:26 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http:///index.html [root@shuai-01 vhost]# cat /tmp/.log 127.0.0.1 - [08/Jan/2018:20:41:20 +0800] test2.com "/index.html" 301 "-" "curl/7.29.0" 127.0.0.1 - [08/Jan/2018:20:41:26 +0800] test3.com "/index.html" 301 "-" "curl/7.29.0" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Nginx日志切割 nginx由于沒有自帶的日志切割工具,在切割日志時,需要借助于系統(tǒng)帶的日志切割工具,或者是自己寫一個日志切割腳本。 自己寫一個日志切割腳本。腳本統(tǒng)一保存/usr/local/sbin/ 先自定義一個腳本: [root@shuai-01 vhost]# vim /usr/local/sbin/nginx_logrotate.sh #! /bin/bash ## 假設nginx的日志存放路徑為/tmp/ d=`date -d "-1 day" +%Y%m%d` #定義切割時間(切割一天前的日志) logdir="/tmp/" #此處指定要切割的日志路徑(該路徑來自虛擬主機配置文件) nginx_pid="/usr/local/nginx/logs/nginx.pid" #調(diào)用pid的目的是執(zhí)行命令:/bin/kill -HUP `cat $nginx_pid` #該命令等價于命令:nginx -s reload(重新加載文件),確保與虛擬主機配置文件變更保持同步 #該地址來自nginx配置文件 cd $logdir for log in `ls *.log` do mv $log $log-$d done #此處使用通配進行循環(huán),并改名字(切割是每天產(chǎn)生的日志重命名) /bin/kill -HUP `cat $nginx_pid` #執(zhí)行此命令進行重載生成新的日志文件來記錄新的日志 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 執(zhí)行腳本: [root@shuai-01 vhost]# sh -x /usr/local/sbin/nginx_logrotate.sh ++ date -d '-1 day' +%Y%m%d + d=20180108 + logdir=/tmp/ + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/ ++ ls .log + for log in '`ls *.log`' + mv .log .log-20180108 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 1513 1 2 3 4 5 6 7 8 9 10 11 -x : 作用是顯示腳本執(zhí)行過程 注意: 這只是對日志進行了切割,對日志進行刪除需要結合任務計劃cron使用。切割也得配合cron使用。 靜態(tài)文件不記錄日志和過期時間 在配置文件中加上配置: 打開配置文件: location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } location ~.*\.(js|css)$ { expires 12h; acces_log off; } [root@shuai-01 vhost]# vim .conf location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #匹配文件類型 { expires 7d; #過期時間為7天 access_log off; #不記錄該類型文件的訪問日志 } location ~ .*\.(js|css)$ { expires 12h; #過期時間為12小時 access_log off; #不記錄該類型文件的訪問日志 } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 檢查配置文件語法并重新加載配置文件: [root@shuai-01 vhost]# /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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload 1 2 3 4 測試: [root@shuai-01 ]# curl -x127.0.0.1:80 /1.gif shjdkjhkasb [root@shuai-01 ]# curl -x127.0.0.1:80 /2.js ajkfdchb [root@shuai-01 ]# curl -x127.0.0.1:80 /index.html [root@shuai-01 ]# cat /tmp/.log 127.0.0.1 - [09/Jan/2018:00:39:45 +0800] "/index.html" 200 "-" "curl/7.29.0" |
|