11-5 12 Nginx訪問日誌 日誌切割 靜態過時

12.10 Nginx訪問日誌css

12.11 Nginx日誌切割html

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

12.10 Nginx訪問日誌

  • 與apache相似,主配置文件中,有定義日誌格式

日誌格式shell

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

  • combined_realip 格式名字,用於調用日誌格式,可改
  • $remote_addr 客戶端IP(公網IP)
  • $http_x_forwarded_for 代理服務器的IP
  • $time_local 服務器本地時間
  • $host 訪問主機名(域名)
  • $request_uri 訪問的url地址
  • $status 狀態碼
  • $http_referer 跳轉來源URL
  • $http_user_agent 訪問工具(瀏覽器參數)

虛擬主機啓用日誌apache

access_log /tmp/ccc.log combined_realip;

  • 這裏的combined_realip就是在nginx.conf中定義的日誌格式類型名稱

-t && -s reload curl -x127.0.0.1:80 ccc.com -I cat /tmp/ccc.log //vim

12.11 Nginx日期切割

  • 沒有apache的切割命令,須要自定義shell 切割腳本
vim /usr/local/sbin/nginx_log_rotate.sh//寫入以下內容
···
#! /bin/bash
# 定義變量d爲昨天日期
d=`date -d "-1 day" +%Y%m%d`
# 定義變量logdir爲日誌路徑,若是不知道能夠看虛擬主機配置
logdir="/tmp/"
#定義nginx_pid爲日誌進程文件
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
# i in commed 將i循環賦值爲命令的每一個輸出,進行do的操做
for i in `ls *.log`
do
    mv $i $i-$d
done
# 文件更名後須要中斷日誌pid,不然新日誌沒法生成
/bin/kill -HUP `cat $nginx_pid`

手動執行(-x顯示執行過程)瀏覽器

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

任務計劃(天天0點執行)bash

crontab -e
...
 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

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

核心配置服務器

location ~ .*(gif|jpg|jpeg|png|bmp|swf)$  //正則精準匹配
{
      expires      7d;   //過時時間
      access_log off;  //不記錄日誌
}
location ~ .*\.(js|css)$
{
      expires      12h;
      access_log off;
}
  • 加載access前面便可

測試curl

[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/1.gif
asfoawnfnasxojfan
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/2.js
soawejifna
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/index.html
This is ccc.com
[root@axiang-02 ccc.com]# cat /tmp/ccc.log  //沒重啓nginx會繼續生成靜態文件日誌
127.0.0.1 - [06/Aug/2017:09:41:47 +0800] ccc.com "/1.gif" 200 "-" "curl/7.29.0"
127.0.0.1 - [06/Aug/2017:09:41:57 +0800] ccc.com "/2.js" 200 "-" "curl/7.29.0"
127.0.0.1 - [06/Aug/2017:09:42:16 +0800] ccc.com "/index.html" 200 "-" "curl/7.29.0"
[root@axiang-02 ccc.com]# /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@axiang-02 ccc.com]# /usr/local/nginx/sbin/nginx -s reload
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/index.html
This is ccc.com
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/2.js
soawejifna
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/1.gif
asfoawnfnasxojfan
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/index.html
This is ccc.com
[root@axiang-02 ccc.com]# !cat
cat /tmp/ccc.log
127.0.0.1 - [06/Aug/2017:09:41:47 +0800] ccc.com "/1.gif" 200 "-" "curl/7.29.0"
127.0.0.1 - [06/Aug/2017:09:41:57 +0800] ccc.com "/2.js" 200 "-" "curl/7.29.0"
127.0.0.1 - [06/Aug/2017:09:42:16 +0800] ccc.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [06/Aug/2017:09:42:57 +0800] ccc.com "/index.html" 200 "-" "curl/7.29.0"
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/1.gif -I
HTTP/1.1 200 OK
...
Cache-Control: max-age=604800   //失效爲7天
相關文章
相關標籤/搜索