Nginx訪問日誌及切割,靜態檔案不記錄日誌和設定過期時間
阿新 • • 發佈:2018-11-28
Nginx訪問日誌
在主配置檔案中檢視當前的日誌格式
搜尋log_format
[[email protected] /]# cd /usr/local/nginx/ [[email protected] nginx]# vim conf/nginx.conf include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format 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_refer | referer
$http_user_agent | user_agent
給虛擬機器定義日誌格式
虛擬主機配置檔案對應的服務配置增加 access_log /tmp/abc.com.log combined_realip;(這裡的combined_realip就是在nginx.conf中定義的日誌格式名字);重新載入配置,測試
[[email protected] nginx]# vim conf/vhost/abc.com.conf
[[email protected] nginx]# cat conf/vhost/abc.com.conf
server
{
listen 80;
server_name abc.com ab.com a.com;
index index.html index.htm index.php;
root /data/wwwroot/abc.com;
if ($host != 'abc.com'){
rewrite ^/(.*)$ http://abc.com/$1 permanent;
}
access_log /tmp/abc.com.log combined_realip; #
}
[ [email protected] 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
[[email protected] nginx]# ./sbin/nginx -s reload
[[email protected] nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com
This is abc.com site.
[[email protected] nginx]# vim conf/vhost/abc.com.conf
[[email protected] nginx]# cat /tmp/abc.com.log
127.0.0.1 - [28/Nov/2018:07:15:06 +0800] abc.com "/" 200 "-" "curl/7.29.0"
Shell指令碼切割日誌
Nginx沒有自帶的日誌切割工具,寫一個指令碼做到同樣的功能。
[[email protected] nginx]# vim /usr/local/sbin/nginx_log_rotate.sh
[[email protected] nginx]# cat /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"
cd $logdir
for log in `ls *.log`
do
mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid` # 使重新生成日誌檔案
[[email protected] nginx]# ls /tmp/
abc.com.log pear php-fcgi.sock web-1
[[email protected] nginx]# sh /usr/local/sbin/nginx_log_rotate.sh
[[email protected] nginx]# ls /tmp/
abc.com.log abc.com.log-20181127 pear php-fcgi.sock web-1
[[email protected] nginx]# date
Wed Nov 28 07:37:54 CST 2018
為指令碼設定定時任務,每天零點執行改指令碼
[[email protected] nginx]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
靜態檔案不記錄日誌及設定過期時間
[[email protected] nginx]# vim conf/vhost/abc.com.conf
[[email protected] nginx]# cat conf/vhost/abc.com.conf
server
{
listen 80;
server_name abc.com ab.com a.com;
index index.html index.htm index.php;
root /data/wwwroot/abc.com;
if ($host != 'abc.com'){
rewrite ^/(.*)$ http://abc.com/$1 permanent;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
access_log /tmp/abc.com.log combined_realip;
}
[[email protected] 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
[[email protected] nginx]# ./sbin/nginx -s reload
[[email protected] nginx]# cat /tmp/abc.com.log
[[email protected] nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com -I
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Tue, 27 Nov 2018 23:59:05 GMT
Content-Type: text/html
Content-Length: 22
Last-Modified: Mon, 26 Nov 2018 21:36:51 GMT
Connection: keep-alive
ETag: "5bfc6773-16"
Accept-Ranges: bytes
[[email protected] nginx]# cat /tmp/abc.com.log
127.0.0.1 - [28/Nov/2018:07:59:05 +0800] abc.com "/" 200 "-" "curl/7.29.0"
[[email protected] nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com/1.jpg -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.1
Date: Tue, 27 Nov 2018 23:59:21 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[[email protected] nginx]# cat /tmp/abc.com.log
127.0.0.1 - [28/Nov/2018:07:59:05 +0800] abc.com "/" 200 "-" "curl/7.29.0"
[[email protected] nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com/1.js -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.1
Date: Wed, 28 Nov 2018 00:00:46 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[[email protected] nginx]# cat /tmp/abc.com.log
127.0.0.1 - [28/Nov/2018:07:59:05 +0800] abc.com "/" 200 "-" "curl/7.29.0"
[[email protected] nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com/1.jpg -I # 新增對應的圖片資源後,可以看到Cache-Control過期時間
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Wed, 28 Nov 2018 00:03:09 GMT
Content-Type: image/jpeg
Content-Length: 4
Last-Modified: Wed, 28 Nov 2018 00:02:59 GMT
Connection: keep-alive
ETag: "5bfddb33-4"
Expires: Wed, 05 Dec 2018 00:03:09 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes