1. 程式人生 > >nginx訪問日誌,日誌切割,靜態文件不記錄日誌

nginx訪問日誌,日誌切割,靜態文件不記錄日誌

-c 增加 app live 主機名 perm exp one html

nginx訪問日誌
  • 日誌格式
    [root@aminglinux-02 nginx]# vim conf/nginx.conf
    log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘
    ‘$host "$request_uri" $status‘
    ‘"$http_referer" "$http_user_agent"‘;
    combined_realip這個是自定義的日誌格式名
    $remote_addr 客戶端ip(公網ip)
    $http_x_forwarded_for 代理服務器ip
    $time_local 服務器的本地時間
    $host 訪問主機名(域名)
    $request_uri 訪問的uri地址
    $status 狀態碼
    $http_referer referer
    $http_user_agent user agent
  • 除了在主配置文件定義日誌格式,還需要在虛擬主機配置文件中增加 access_log /tmp/1.log combined_realip;
    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/1.log combined_realip;
    }
    • 這裏的combined_realip是主配置文件中定義的日誌格式名

      Nginx日誌切割

  • 編寫一個日誌切割腳本
    [root@aminglinux-02 ~]# 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`
  • 腳本存放路徑/usr/local/sbin
  • d變量定義的是切割前一天日期,logdir是日誌的路徑
  • 用一個for循環來遍歷所有的虛擬主機日誌
  • 最後一行是生成新的日誌文件
  • 清除過期的日誌文件命令
    [root@aminglinux-02 ~]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
  • 任務計劃,每天淩晨執行
    [root@aminglinux-02 ~]# crontab -e
    no crontab for root - using an empty one
    0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh

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

  • 虛擬主機配置如下
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$  #匹配.gif等文件
    {
          expires      7d;    #過期時間,如果沒有這個,這兩個可以寫在一個location裏
          access_log off;
    }
    location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }
  • 測試
    [root@akuilinux01 test.com]# vim 1.gif
    [root@akuilinux01 test.com]# vim 2.js
    [root@akuilinux01 test.com]# curl -x127.0.0.1:80 test.com/1.gif
    dkajdkaj
    [root@akuilinux01 test.com]# curl -x127.0.0.1:80 test.com/2.js
    26376732
    [root@akuilinux01 test.com]# curl -x127.0.0.1:80 test.com/
    <html>
    <head><title>401 Authorization Required</title></head>
    <body bgcolor="white">
    <center><h1>401 Authorization Required</h1></center>
    <hr><center>nginx/1.14.0</center>
    </body>
    </html>
    [root@akuilinux01 test.com]# cat /tmp/nginx_access.log
    127.0.0.1 - [16/Jun/2018:11:05:31 +0800] test.com "/" 401 "-" "curl/7.29.0"
    [root@akuilinux01 test.com]# curl -x127.0.0.1:80 test.com/2.jsdjakjk
    <html>
    <head><title>401 Authorization Required</title></head>
    <body bgcolor="white">
    <center><h1>401 Authorization Required</h1></center>
    <hr><center>nginx/1.14.0</center>
    </body>
    </html>
    [root@akuilinux01 test.com]# cat /tmp/nginx_access.log
    127.0.0.1 - [16/Jun/2018:11:05:31 +0800] test.com "/" 401 "-" "curl/7.29.0"
    127.0.0.1 - [16/Jun/2018:11:06:55 +0800] test.com "/2.jsdjakjk" 401
    以.js和.gif結尾的不記錄,其他的記錄
  • max-age=43200這個是過期時間
    [root@akuilinux01 test.com]# curl -x127.0.0.1:80 test.com/2.js -I
    HTTP/1.1 200 OK
    Server: nginx/1.14.0
    Date: Sat, 16 Jun 2018 03:07:14 GMT
    Content-Type: application/javascript
    Content-Length: 9
    Last-Modified: Sat, 16 Jun 2018 03:04:31 GMT
    Connection: keep-alive
    ETag: "5b247e3f-9"
    Expires: Sat, 16 Jun 2018 15:07:14 GMT
    Cache-Control: max-age=43200
    Accept-Ranges: bytes

nginx訪問日誌,日誌切割,靜態文件不記錄日誌