Nginx訪問日誌
-
日誌格式配置
[root@localhost ~]# 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(公網IP) |
$http_x_forwarded_for | 代理服務器的IP |
$time_local | 服務器本地時間 |
$host | 訪問主機名(域名) |
$request_uri | 訪問的URL地址 |
$status | 狀態碼 |
$http_referer | referer |
$http_user_agent | user_agent |
- 定義虛擬主機的日誌格式
定義虛擬主機的前提是在Nginx配置文件中設定日誌格式,然後才能在虛擬主機中進行調用(格式名稱)
定義test.com.conf的日誌格式:
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
......
access_log /tmp/test.com.log combined_realip;
......
# 指定日誌位置及格式(格式名稱)
檢測並重新加載配置:
[root@localhost ~]# /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@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
-
測試
訪問test2.com:
[root@localhost ~]# curl -x127.0.0.1 test2.com -I
curl: (7) Failed connect to 127.0.0.1:1080; 拒絕連接
[root@localhost ~]# curl -x127.0.0.1:80 test2.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Thu, 04 Jan 2018 13:00:43 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/訪問test.com:
[root@localhost ~]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Thu, 04 Jan 2018 13:00:54 GMT
Content-Type: text/html
Content-Length: 9
Last-Modified: Wed, 03 Jan 2018 14:45:43 GMT
Connection: keep-alive
ETag: "5a4cec97-9"
Accept-Ranges: bytes查看訪問日誌:
[root@localhost ~]# cat /tmp/test.com.log
127.0.0.1 - [04/Jan/2018:21:00:43 +0800] test2.com "/" 301 "-" "curl/7.29.0"
127.0.0.1 - [04/Jan/2018:21:00:54 +0800] test.com "/" 200 "-" "curl/7.29.0"
Nginx日誌切割
因爲Nginx沒有自帶的日誌切割工具,所以需要藉助系統日誌切割命令或使用日誌切割腳本。
-
日誌切割腳本
爲了方便管理,shell腳本統一保存位置:/usr/local/sbin/
編輯腳本:
[root@localhost ~]# 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的目的是執行命令:/bin/kill -HUPcat $nginx_pid
#該命令等價於命令:nginx -s reload(重新加載文件),確保與虛擬主機配置文件變更保持同步
#該地址來自nginx配置文件
cd $logdir
for log inls *.log
do
mv $log $log-$d
done
#此處使用通配進行循環,對所有複合條件的日誌文件進行切割
/bin/kill -HUPcat $nginx_pid
#執行此命令進行重載生成新的日誌文件來記錄新的日誌執行腳本:
[root@localhost ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d- d=20180103
- 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-20180103
- for log in '
ls *.log
' - mv yum.log yum.log-20180103
++ cat /usr/local/nginx/logs/nginx.pid - /bin/kill -HUP 1313
注意: -x 選項的作用是顯示腳本執行過程。該腳本配合任務計劃cron使用,定期進行切割和清理。
靜態文件不記錄日誌和過期時間
-
核心配置參數:
配置虛擬主機配置文件:
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/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@localhost ~]# /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@localhost ~]# /usr/local/nginx/sbin/nginx -s reload -
測試
訪問test.com:
[root@localhost ~]# curl -x127.0.0.1:80 test.com
test.com
[root@localhost ~]# cat /tmp/test.com.log
127.0.0.1 - [04/Jan/2018:23:39:40 +0800] test.com "/" 200 "-" "curl/7.29.0"
#有訪問日誌訪問 abc。jpg
[root@localhost ~]# curl -x127.0.0.1:80 test.com/abc.jpg -I
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Thu, 04 Jan 2018 15:41:59 GMT
Content-Type: image/jpeg
Content-Length: 247902
Last-Modified: Thu, 04 Jan 2018 15:41:12 GMT
Connection: keep-alive
ETag: "5a4e4b18-3c85e"
Expires: Thu, 11 Jan 2018 15:41:59 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytesmax-age=604800s=7天,即該文件緩存的過期時間爲7天!
[root@localhost ~]# cat /tmp/test.com.log
127.0.0.1 - [04/Jan/2018:23:39:40 +0800] test.com "/" 200 "-" "curl/7.29.0"返回值爲200,訪問成功,但無訪問日誌。