2018-6-8

12.10 Nginx訪問日誌

12.11 Nginx日誌切割

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




12.10 Nginx訪問日誌

日誌配置文件

 vim /usr/local/nginx/conf/nginx.conf //搜索log_format

image.png

這一段就是用來定義日誌的 ,combined_realip是日誌名字可以隨意改變

相關參數的含義

image.png

除了在主配置文件nginx.conf裏定義日誌格式外,還需要在虛擬主機配置文件中增加

vim test.com.conf

 access_log /tmp/1.log combined_realip;  //定義日誌格式

image.png

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

 -t && -s reload

 curl -x127.0.0.1:80 test.com -I

 cat /tmp/1.log






12.11 Nginx日誌切割


自定義shell 腳本

 vim /usr/local/sbin/nginx_log_rotate.sh//寫入如下內容

#! /bin/bash

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

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`

 任務計劃

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

image.png


sh -x /usr/local/sbin/nginx_logrotate.sh 




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

同樣,不記錄一些靜態的圖片燈

配置如下

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

          expires      7d;

          access_log off;

    }

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

    {

          expires      12h;

          access_log off;

    }


image.png

location 是匹配 ,\脫譯,爲了精準匹配防止出錯

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