day 49 Nginx訪問日誌

12.10 Nginx訪問日誌

1、Nginx日誌格式
  • vim /usr/local/nginx/conf/nginx.conf                        #編輯nginx配置文件,搜索log_format,定義nginx日誌格式
$remote_addr
客戶端IP(公網IP)
$http_x_forwarded_for
代理服務器的IP
$time_local
服務器本地時間
$host
訪問主機名(域名)
$request_uri
訪問的url地址
$status
狀態碼
$http_referer
referer
$http_user_agent
user_agent
除了在主配置文件nginx.conf裏定義日誌格式外,還需要在虛擬主機配置文件/usr/local/nginx/conf/vhost/test.com.conf中增加如下內容:
access_log /tmp/1.log combined_realip; #combined_realip是在nginx.conf中定義的日誌格式名字
  • -t && -s reload                                    #語法檢查並重加載配置
  • curl -x127.0.0.1:80 test.com -I           #curl命令測試
  • cat /tmp/1.log                                     #查看日誌
12.11 Nginx日誌切割

  • vim /usr/local/sbin/nginx_log_rotate.sh                   #自定義切割日誌功能的shell 腳本,寫入如下內容
#! /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`
  • 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh                          #添加計劃任務
12.12 靜態文件不記錄日誌和過期時間 

  • 靜態文件不記錄日誌和過期時間,需要編輯nginx配置文件/usr/local/nginx/conf/nginx.conf,添加如下內容:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }
location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }

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