訪問日志
Nginx日志格式:
[root@123 ~]# vim /usr/local/nginx/conf/nginx.conf log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"';
說明:
“combined_ realip”:日志格式名稱;'$remote_ addr $http_ x_ forwarded_ for [$time_ local]' ' $host "$request_uri" $status' ' "$http_ referer" "$http_ user_ agent"' :日志內容。
注釋:
名稱 | 含義 |
---|---|
$remote_addr | 客戶端IP(公網(wǎng)IP) |
$http_x_forwarded_for | 代理服務器的IP |
$time_local | 服務器本地時間 |
$host | 訪問主機名(域名) |
$request_uri | 訪問的URL地址 |
$status | 狀態(tài)碼 |
$http_referer | referer |
$http_user_agent | user_agent |
定義虛擬主機日志格式
定義虛擬主機的前提是在Nginx配置文件中設定日志格式,然后才能在虛擬主機中進行調用(格式名稱)。
[root@123 ~]# cd /usr/local/nginx/conf/vhost/ [root@123 vhost]# ls aaa.com.conf test.com.conf 定義test.com.conf日志格式: [root@123 vhost]# vim test.com.conf …… access_log /tmp/test.com.log combined_realip; #指定日志位置及格式 檢查錯誤: [root@123 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
注:?如果不指定日志格式,系統(tǒng)使用默認日志格式,記錄內容較簡單。
檢測:
[root@123 vhost]# curl -x127.0.0.1:80 test.com This is test.com [root@123 vhost]# cat /tmp/test.com.log 127.0.0.1 - [11/Aug/2017:15:09:54 +0800] test.com "/" 200 "-" "curl/7.29.0"
?Nginx日志切割
因為Nginx沒有自帶的日志切割工具,所以需要借助系統(tǒng)日志切割命令或使用日志切割腳本。
日志切割腳本
為了方便管理,shell腳本統(tǒng)一保存位置:/usr/local/sbin/
[root@123 vhost]# vim /usr/local/sbin/nginx_log_rotate.sh #! /bin/bash d=`date -d "-1 day" +%Y%m%d` #定義切割時間(切割一天前的日志) logdir="/tmp/" #此處指定要切割的日志路徑(該路徑來自虛擬主機配置文件) nginx_pid="/usr/local/nginx/logs/nginx.pid" #調用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),對所有復合條件的日志文件進行切割 /bin/kill -HUP `cat $nginx_pid` #執(zhí)行此命令進行重載生成新的日志文件來記錄新的日志
執(zhí)行該腳本:
[root@123 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh ++ date -d '-1 day' +%Y%m%d + d=20170810 + logdir=/tmp/ + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/ ++ ls test.com.log yum.log + for log in '`ls *.log`' + mv test.com.log test.com.log-20170810 + for log in '`ls *.log`' + mv yum.log yum.log-20170810 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 1251
說明:?-x選項的作用是顯示腳本執(zhí)行過程。
注:?該腳本配合任務計劃cron使用,定期進行切割和清理。
靜態(tài)文件不記錄日志&過期時間
核心配置參數(shù):
[root@123 vhost]# vim test.com.conf location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ #匹配文件類型 { expires 7d; #過期時間為7天 access_log off; #不記錄該類型文件的訪問日志 } location ~ .*.(js|css)$ { expires 12h; #過期時間為12小時 access_log off; #不記錄該類型文件的訪問日志 } access_log /tmp/test.com.log combined_realip; #指定日志位置及格式
檢測:
[root@123 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@123 vhost]# /usr/local/nginx/sbin/nginx -s reload 訪問index.html: [root@123 vhost]# !curl curl -x127.0.0.1:80 test.com This is test.com [root@123 vhost]# !cat cat /tmp/test.com.log 127.0.0.1 - [11/Aug/2017:17:45:28 +0800] test.com "/" 200 "-" "curl/7.29.0" 即:有日志! 訪問baidu.png文件: [root@123 test.com]# curl -x127.0.0.1:80 test.com/baidu.png -I HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Fri, 11 Aug 2017 09:47:57 GMT Content-Type: image/png Content-Length: 3706 Last-Modified: Tue, 01 Aug 2017 10:13:45 GMT Connection: keep-alive ETag: "59805459-e7a" Expires: Fri, 18 Aug 2017 09:47:57 GMT Cache-Control: max-age=604800 Accept-Ranges: bytes 說明:max-age=604800s=7天,即該文件緩存的過期時間為7天! [root@123 test.com]# cat /tmp/test.com.log 127.0.0.1 - [11/Aug/2017:17:45:28 +0800] test.com "/" 200 "-" "curl/7.29.0" 即:無該文件的訪問日志!?。?/pre>