1. 程式人生 > >Nginx訪問日誌、Nginx日誌切割、靜態文件不記錄日誌和過期時間介紹

Nginx訪問日誌、Nginx日誌切割、靜態文件不記錄日誌和過期時間介紹

Linux

Nginx訪問日誌


1. 進入配置文件

[root@gary-tao src]# vim /usr/local/nginx/conf/nginx.conf //搜索log_format


參考更改配置成如下:

log_format aming '$remote_addr $http_x_forwarded_for [$time_local]'

如圖:

技術分享圖片



日誌格式字段含義如下:

combined_realip為日誌格式的名字,後面可以調用它。

技術分享圖片


2.到虛擬主機配置文件中指定訪問日誌的路徑

[root@gary-tao src]# vim /usr/local/nginx/conf/vhost/test.com.conf


增加如下內容:

access_log /tmp/test.com.log combined_realip;

//這裏的combined_realip就是在nginx.conf中定義的日誌格式名字

如圖:

技術分享圖片


3.測試語法及重新加載配置

[root@gary-tao src]# /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@gary-tao src]# /usr/local/nginx/sbin/nginx -s reload


4.使用curl測試

[root@gary-tao src]# curl -x127.0.0.1:80 test.com -I

HTTP/1.1 200 OK

Server: nginx/1.12.1

Date: Thu, 04 Jan 2018 09:19:09 GMT

Content-Type: text/html

Content-Length: 15

Last-Modified: Wed, 03 Jan 2018 13:03:42 GMT

Connection: keep-alive

ETag: "5a4cd4ae-f"

Accept-Ranges: bytes


[root@gary-tao src]# curl -x127.0.0.1:80 test2.com/admin -I

HTTP/1.1 301 Moved Permanently

Server: nginx/1.12.1

Date: Thu, 04 Jan 2018 09:19:54 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http://test.com/admin


[root@gary-tao src]# curl -x127.0.0.1:80 test2.com/admin/index.html -I

HTTP/1.1 301 Moved Permanently

Server: nginx/1.12.1

Date: Thu, 04 Jan 2018 09:20:04 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http://test.com/admin/index.html


[root@gary-tao src]# cat /tmp/test.com.log

127.0.0.1 - [04/Jan/2018:17:19:09 +0800] test.com "/" 200 "-" "curl/7.29.0"

127.0.0.1 - [04/Jan/2018:17:19:54 +0800] test2.com "/admin" 301 "-" "curl/7.29.0"

127.0.0.1 - [04/Jan/2018:17:20:04 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0"



Nginx日誌切割

1.自定義一個腳本

[root@gary-tao src]# vim /usr/local/sbin/nginx_log_rotate.sh


定義如下內容:

#!/bin/bash

## 假設nginx的日誌存放路徑為/data/logs/

d=`date -d "-1 day" +%Y%m%d` //這個日期是昨天的日期,因為日誌切割是第二天才執行這個腳本的。

logdir="/data/logs"

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` //跟Nginx的-s重新加載配置文件一樣


2.執行腳本

sh執行,-x是顯示執行的過程


[root@gary-tao src]# 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 php_errors.log test.com.log

+ for log in '`ls *.log`'

+ mv php_errors.log php_errors.log-20180103

+ for log in '`ls *.log`'

+ mv test.com.log test.com.log-20180103

++ cat /usr/local/nginx/logs/nginx.pid

+ /bin/kill -HUP 62748


[root@gary-tao src]# ls /tmp/

mysql.sock systemd-private-b666888e47f84d62afce0dcb90bdfc91-vmtoolsd.service-09q5L2

pear systemd-private-fdc53ff508e94ecda3c5a90dad98a792-vmtoolsd.service-O5O620

php_errors.log-20180103 test.com.log

php-fcgi.sock test.com.log-20180103

systemd-private-6fc6799999fe42dd97426bde338fb145-vmtoolsd.service-qi1M6k


3.任務計劃

[root@gary-tao src]# crontab -e //添加任務計劃

增加如下內容:

0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh



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

1.修改虛擬主機配置文件

[root@gary-tao src]# vim /usr/local/nginx/conf/vhost/test.com.conf


增加如下內容:

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ //匹配脫義靜態文件

{

expires 7d; //配置過期時間

access_log off;

}

location ~ .*\.(js|css)$ //匹配js,css文件

{

expires 12h;

access_log off;

}


如圖:

技術分享圖片


2.測試語法及重新加載配置

[root@gary-tao src]# /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@gary-tao src]# /usr/local/nginx/sbin/nginx -s reload


3.使用curl測試

[root@gary-tao src]# cd /data/wwwroot/test.com

[root@gary-tao test.com]# ls

admin index.html

[root@gary-tao test.com]# vim 1.gif

[root@gary-tao test.com]# echo "dgagadgadgs" > /data/wwwroot/test.com/2.js

[root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/1.gif

dggagadggagdag

[root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/2.js

dgagadgadgs

[root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/index.html

“test.com”

[root@gary-tao test.com]# cat /tmp/test.com.log

127.0.0.1 - [04/Jan/2018:18:53:20 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

[root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/index.html

“test.com”

[root@gary-tao test.com]# cat /tmp/test.com.log

127.0.0.1 - [04/Jan/2018:18:53:20 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

127.0.0.1 - [04/Jan/2018:18:53:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

[root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/2.js

dgagadgadgs

[root@gary-tao test.com]# cat /tmp/test.com.log

127.0.0.1 - [04/Jan/2018:18:53:20 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

127.0.0.1 - [04/Jan/2018:18:53:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

[root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/2.jsagdaga

<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@gary-tao test.com]# cat /tmp/test.com.log

127.0.0.1 - [04/Jan/2018:18:53:20 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

127.0.0.1 - [04/Jan/2018:18:53:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

127.0.0.1 - [04/Jan/2018:18:55:22 +0800] test.com "/2.jsagdaga" 404 "-" "curl/7.29.0"

[root@gary-tao test.com]# curl -x127.0.0.1:80 -I test.com/2.js

HTTP/1.1 200 OK

Server: nginx/1.12.1

Date: Thu, 04 Jan 2018 10:56:11 GMT

Content-Type: application/javascript

Content-Length: 12

Last-Modified: Thu, 04 Jan 2018 10:51:59 GMT

Connection: keep-alive

ETag: "5a4e074f-c"

Expires: Thu, 04 Jan 2018 22:56:11 GMT

Cache-Control: max-age=43200 //43200秒表示過期時間12小時,與配置文件裏一樣。

Accept-Ranges: bytes


Nginx訪問日誌、Nginx日誌切割、靜態文件不記錄日誌和過期時間介紹