1. 程式人生 > >Nginx訪問日誌&日誌切割&靜態檔案

Nginx訪問日誌&日誌切割&靜態檔案

[toc]

Nginx訪問日誌&日誌切割&靜態檔案

12.10 Nginx訪問日誌

1 開啟配置檔案:

vim /usr/local/nginx/conf/vhost/../nginx.conf

[[email protected] vhost]# vim ../nginx.conf

mark

找到如下,是定義日誌格式:

log_format xavi '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';

combined_realip;為日誌格式名字,可以改為其他的,後面可以呼叫它 上述定義日誌的名稱改為xavi。 注:這邊改成什麼名字,待會引用的時候就寫成什麼!

2. 格式分析:

mark

3 虛擬主機中定義訪問日誌路徑

如上除了在主配置檔案nginx.conf裡定義日誌格式外,還需要在虛擬主機配置檔案中增加:

access_log /tmp/atorreid.log xavi; 使用access_log指定日誌儲存路徑和使用的日誌格式名字

[[email protected] vhost]# vim atorreid.com.conf

server
{
    listen 80 default_server;
    server_name atorreid.com xavi.com abc.com;
    index index.html index.htm index.php;
    root /data/nginx/www.torreid.com;
    if ($host != 'torreid.com' ) {
        rewrite  ^/(.*)$  http://torreid.com/$1  permanent;
    }
    access_log /tmp/test.com.log xavi
     location /
     {
       auth_basic         "Auth";
       auth_basic_user_file /usr/local/nginx/conf/htpasswd;
     }
}
    

4. 訪問測試

[[email protected] vhost]# /usr/local/nginx/sbin/nginx -s reload
[[email protected] vhost]# !curl
curl -x127.0.0.1:80 www.atorreid.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Wed, 14 Mar 2018 16:05:55 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://torreid.com/index.html

12.12 Nginx 日誌切割

1. 為什麼需要日誌切割

日誌對於統計排錯來說非常有利的,如果一個100G的日誌別說查看了就開啟我們都需要等待很久這樣不僅浪費了我們的硬體資源同時也浪費了時間。如果按照每天分成一個日誌,是不是更有利於我們去排障呢?-- by ZDY

由於Nginx不像Apache有自己的切割工具,在此我們需要寫個指令碼完成需求:

2.編輯shell指令碼,執行日誌切割

養成好的習慣把指令碼放在sbin目錄下:

vim /usr/local/sbin/nginx_logrotate.sh

[[email protected] ~]# vim /usr/local/sbin/nginx_logrotate.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`

d=date -d "-1 day" +%Y%m%d;生成昨天的日期 for log in ls *.log do mv $log $log-$d done 這是一個for迴圈,把ls列舉的log檔案,執行以日期格式的重新命名 nginx_pid=”/usr/local/nginx/logs/nginx.pid”;就是為了最後一行而設定的。

/bin/kill -HUP cat $nginx_pid 最後一行的意思和之前使用的 -s reload 是一個意思 過載nginx.pid,然後就會再次生成一個新的日誌檔案。否則不生成日誌檔案

sh -x /usr/local/sbin/nginx_logrotate.sh檢視shell指令碼執行過程

[[email protected] ~]# sh -x /usr/local/sbin/nginx_logrotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180314
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls atorreid.com.log ' php_errors.log' php_errors.log test.com.log torrecid.com.log
+ for log in '`ls *.log`'
+ mv atorreid.com.log atorreid.com.log-20180314
+ for log in '`ls *.log`'
+ mv php_errors.log php_errors.log-20180314
+ for log in '`ls *.log`'
+ mv php_errors.log php_errors.log-20180314
mv: 無法獲取"php_errors.log" 的檔案狀態(stat): 沒有那個檔案或目錄
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180314
+ for log in '`ls *.log`'

mark

3.日誌清理

有了切割可以滿足我們的日常工作需要,但是隨著訪問量的劇增,如果不刪除老的日誌檔案我們的磁碟很快就會佔用完。

  • 刪除超過一個月的日誌(當然這個也可以寫在腳本里面)
[[email protected] ~]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm

4.建立計劃任務

crontab -e

加入如下內容 0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh

[[email protected] ~]# crontab -e
no crontab for root - using an empty one

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

完整示例:針對每天的慢日誌進行日誌切割:

轉載自老七部落格:http://www.okay686.cn/524.html

#! /bin/bash

d=`date -d "-1 day" +%Y%m%d`
logdir="/usr/local/php-fpm/var/log/"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `find . -name "*_slow.log"`
do
    mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`

find . -name "*_slow.log-*" -mtime +30 | xargs rm -rf

12.12 靜態檔案不記錄日誌和過期時間

1. 虛擬主機配置檔案location~可以指定對應的靜態檔案,expires配置過期時間,而access_log 配置為off就可以不記錄訪問日誌了

[[email protected] ~]# cd /usr/local/nginx/conf/vhost/
[[email protected] vhost]# ls
atorreid.com.conf  bcd.com.conf  test.com.conf
[[email protected] vhost]# vim test.com.conf
server
{
    listen 80 default_server;
    server_name test.com xavi.com abc.com;
    index index.html index.htm index.php;
    root /data/nginx/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.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/test.com.log xavi;

}
  • 詳解:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }
    
~匹配.gif  .jpg  .jpeg等格式的靜態檔案不計入日誌,|表示或者
\表示脫義字元,XXX.gif

2.測試

建立gif和js字尾名的檔案

[[email protected] vhost]# cd /data/nginx/test.com/
[[email protected] test.com]# ls
admin  admin.php  index.html
[[email protected] test.com]# vim 1.gif
[[email protected] test.com]# vim 2.js

訪問1.gif和2.js,

[[email protected] test.com]# curl -x127.0.0.1:80 test.com/2.js
axccdecdcece:
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/1.gif
dadadsdsda
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/x.gif
<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>

然後檢視日誌檔案

[[email protected] test.com]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:21:09:12 +0800] www.atorreid.com "/index.html" 301 "-" "curl/7.29.0"
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/xx2.jsdsdsad
<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>
[[email protected] test.com]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:21:09:12 +0800] www.atorreid.com "/index.html" 301 "-" "curl/7.29.0"
127.0.0.1 - [15/Mar/2018:21:12:25 +0800] test.com "/xx2.jsdsdsad" 404 "-" "curl/7.29.0"
127.0.0.1 - [15/Mar/2018:21:15:33 +0800] test.com "/2.jsdsdsad" 404 "-" "curl/7.29.0"

自定義了一個2.jsdsdsad被記錄到日誌,但是1.js和2.jpg均沒有被計入到日誌,對應在test.com.conf的location定義

3.測試過期時間:

[[email protected] test.com]# curl -x127.0.0.1:80 test.com/2.js -I //訪問js型別的檔案,快取過期時間為12小時
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 15 Mar 2018 13:17:07 GMT
Content-Type: application/javascript
Content-Length: 14
Last-Modified: Thu, 15 Mar 2018 13:08:00 GMT
Connection: keep-alive
ETag: "5aaa7030-e"
Expires: Fri, 16 Mar 2018 01:17:07 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

max-age=43200 對應的是test.com.conf裡對Expires的定義12小時