1. 程式人生 > >12.10 Nginx訪問日誌 12.11 Nginx日誌切割 12.12 靜態文件不記錄日誌和過期

12.10 Nginx訪問日誌 12.11 Nginx日誌切割 12.12 靜態文件不記錄日誌和過期

12.10 Nginx訪問日誌 12.1

12.10 Nginx訪問日誌

技術分享圖片

[root@martin001 vhost]# vim test.com.conf
除了在主配置文件nginx.conf裏定義日誌格式外,還需要在虛擬主機配置文件中增加
access_log /tmp/test.com.log martin;
這裏的combined_realip就是在nginx.conf中定義的日誌格式名字

[root@martin001 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@martin001 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@martin001 vhost]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Wed, 14 Mar 2018 15:39:14 GMT
Content-Type: text/html
Content-Length: 9
Last-Modified: Tue, 13 Mar 2018 16:20:55 GMT
Connection: keep-alive
ETag: "5aa7fa67-9"
Accept-Ranges: bytes

[root@martin001 vhost]# cat /tmp/test.com.log
127.0.0.1 - [14/Mar/2018:23:34:33 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:23:39:14 +0800] test.com "/" 200 "-" "curl/7.29.0"

12.11 Nginx日誌切割

技術分享圖片

[root@martin001 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"
cd $logdir
for log in ls *.log
do
mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid

[root@martin001 vhost]# ls /usr/local/nginx/logs/nginx.pid
/usr/local/nginx/logs/nginx.pid

[root@martin001 vhost]# ls /tmp/
mysql.sock
pear
php-fcgi.sock
systemd-private-d3d5de9693ed4f8ebbf3d1f91826a8cd-chronyd.service-zjb45v
systemd-private-d3d5de9693ed4f8ebbf3d1f91826a8cd-vgauthd.service-evgZ7p
systemd-private-d3d5de9693ed4f8ebbf3d1f91826a8cd-vmtoolsd.service-twe9Yk
test.com.log
test.com.log-20180313

[root@martin001 vhost]# crontab -e
no crontab for root - using an empty one

任務計劃 crontab -e
0 0 * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

12.12 靜態文件不記錄日誌和過期時間

技術分享圖片

[root@martin001 vhost]# cat test.com.conf
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != ‘test.com‘ ) {
rewrite ^/(.)$ http://test.com/$1 permanent;
}
access_log /tmp/test.com.log martin;
location ~ .
.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*.(js|css)$
{
expires 12h;
access_log off;
}

}

[root@martin001 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@martin001 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@martin001 vhost]# cd /data/wwwroot/test.com/
[root@martin001 test.com]# ls
index.html
[root@martin001 test.com]# vim 1.gif
[root@martin001 test.com]# vim 2.js
[root@martin001 test.com]# curl -x127.0.0.1:80 test.com/1.gif
dfgsdfgsdfgsdfg
[root@martin001 test.com]# curl -x127.0.0.1:80 test.com/2.js
dfasdfasdfasdf
[root@martin001 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@martin001 test.com]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:00:19:27 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@martin001 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@martin001 test.com]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:00:19:27 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [15/Mar/2018:00:20:51 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@martin001 test.com]# curl -x127.0.0.1:80 test.com/2.jsghfgfh
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@martin001 test.com]# curl -x127.0.0.1:80 test.com/2.js -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Wed, 14 Mar 2018 16:21:27 GMT
Content-Type: application/javascript
Content-Length: 15
Last-Modified: Wed, 14 Mar 2018 16:18:11 GMT
Connection: keep-alive
ETag: "5aa94b43-f"
Expires: Thu, 15 Mar 2018 04:21:27 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

[root@martin001 test.com]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:00:19:27 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [15/Mar/2018:00:20:51 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [15/Mar/2018:00:21:03 +0800] test.com "/2.jsghfgfh" 404 "-" "curl/7.29.0"

12.10 Nginx訪問日誌 12.11 Nginx日誌切割 12.12 靜態文件不記錄日誌和過期