LNMP架構(nginx訪問日誌,Nginx日誌切割,靜態文件不記錄訪問日誌)

一、nginx訪問日誌

[root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf           (修改Nginx的配置文件)

搜索/log_format  (log_format後面跟的combined_realip是一個自定義名字,用來定義整個日誌格式,這裏寫什麼,虛擬配置文件後面就可以加上什麼,我這裏將combined_realip修改爲lty)

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

    ' $host "$request_uri" $status'

    ' "$http_referer" "$http_user_agent"';

1.png

[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf           (修改虛擬機文件)

access_log /tmp/test.com.log lty;                   (增加一行,lty是剛在主配置文件裏寫的日誌格式)

 

檢查語法錯誤並且重新加載配置文件:

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


檢測;

[root@lnmp ~]# curl -x127.0.0.1:80 test2.com/admin/1.php -I

HTTP/1.1 301 Moved Permanently

Server: nginx/1.8.0

Date: Thu, 14 Dec 2017 05:32:49 GMT

Content-Type: text/html

Content-Length: 184

Connection: keep-alive

Location: http://test.com/admin/1.php


[root@lnmp ~]# curl -x127.0.0.1:80 test.com/admin/1.php 

touch file.php


[root@lnmp ~]# cat /tmp/test.com.log                  (查看日誌)

127.0.0.1 - [14/Dec/2017:13:32:49 +0800] test2.com "/admin/1.php" 301 "-" "curl/7.29.0"

127.0.0.1 - [14/Dec/2017:13:33:10 +0800] test.com "/admin/1.php" 200 "-" "curl/7.29.0"


二、nginx的日誌切割

nginx日誌切割

nginx沒有像httpd一樣,自己帶有切割工具,則需要藉助系統的切割工具或者自己寫一個切割的腳本

[root@lnmp ~]# vim /usr/local/sbin/nginx_log_rotate.sh                    (寫一個自主切割的腳本)

#!/bin/bash

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

d=`date -d "-1 day" +%Y%m%d`                   (生成一個年月日day -1的日期,(昨天的日期))

logdir="/tmp/"                          (定義logdir爲/tmp)

nginx_pid="/usr/local/nginx/logs/nginx.pid"        (給Nginx.pid定義一個變量,爲下面命令做準備)

cd $logdir                               (進入到logdir中)

for log in `ls *.log`                        (做一個for循環,ls當前目錄下所有以.log文件爲結尾的文件)

do                                       

    mv $log $log-$d                         (把以log爲結尾的日誌名都改成log---日期)

done

/bin/kill -HUP `cat $nginx_pid`                 (重新啓動nginx_pid進程,重新生成一個test.com.log文件)      


執行腳本:

[root@lnmp ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh 

++ date -d '-1 day' +%Y%m%d

+ d=20171213

+ logdir=/tmp/

+ nginx_pid=/usr/local/nginx/logs/nginx.pid

+ cd /tmp/

++ ls test.com.log

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

+ mv test.com.log test.com.log-20171213

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

+ /bin/kill -HUP 1157


[root@lnmp ~]# ll /tmp/

srwxrwxrwx. 1 mysql mysql  0 12月 14 11:56 mysql.sock

srw-rw-rw-. 1 root  root   0 12月 14 11:55 php-fcgi.sock

drwx------. 3 root  root  17 12月 14 11:55 systemd-private-50670dd070a94a6f85f2f82feb779c46-vmtoolsd.service-vZTOZz

-rw-r--r--. 1 root  root   0 12月 14 13:54 test.com.log

-rw-r--r--. 1 root  root   0 12月 14 13:54 test.com.log-20171213


最後一步,添加任務計劃:

[root@lnmp ~]# crontab -e

no crontab for root - using an empty one


0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh        (添加一行)


三、靜態文件不記錄日誌

[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$    (以gif,jpg,jpeg,png,bmp,swf結尾的文件保存7天,並且不記錄日誌)

    {

          expires      7d;

          access_log off;

    }

location ~ .*\.(js|css)$

    {

          expires      12h;   (以js,css結尾的文件保存12小時,並且不記錄日誌)

          access_log off;

    }

檢查語法並且重新加載配置文件:

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


證明:分別訪問了以gif,js,html爲結尾的3個文件,發現日誌裏只記錄了html爲結尾的訪問信息。

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

dasdasdafasdfaf

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

fdasfsadfasdfzczv

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

test.com

[root@lnmp ~]# cat /tmp/test.com.log

127.0.0.1 - [14/Dec/2017:14:30:40 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章