nginx 日誌切割

2021-07-26 18:38:14 字數 1433 閱讀 9667

直接在nginx配置檔案中,配置日誌迴圈,而不需使用logrotate或配置cron任務。需要使用到$time_iso8601 內嵌變數來獲取時間。$time_iso8601格式如下:2015-08-07t18:12:02+02:00。然後使用正規表示式來獲取所需時間的資料。

使用下面的**塊

1

2

3

4

5

6

7 if(

$time_iso8601

~"^(\d)-(\d)-(\d)"

)

access_log

/data

/logs

/nginx

/www

.ttlsa

.com-$

year-$

month-$

day-

access

.log

;

也可以使用perl語法來捕獲,如下: 1

2

3 if

($time_iso8601

~"^(?\d)-(?\d)-(?\d)")

access_log

/data

/logs

/nginx

/www

.ttlsa

.com-$

year-$

month-$

day-

access

.log;

1

2

3

4

5

6

7

8

9 if

($time_iso8601

~"^(\d)-(\d)-(\d)t(\d):(\d):(\d)")

非常方便的進行日誌分割。建議按小時分割日誌,方便分析查詢日誌。(此句可省略,未體驗到方便分析查詢日誌的好處)

事例:   

server )-(\d)-(\d)t(\d):(\d):(\d)")

#access_log  logs/host.access.log  main;

add_header "x-ua-compatible" "ie=edge, chrome=1";

location /

location /front/product/toproduct

location /front/productlist/toproductlist

location /front/productlist/queryproducts

location /customer/login

nginx日誌切割

web 訪問日誌 access log 記錄了所有外部客戶端對web伺服器的訪問行為,包含了客戶端ip,訪問日期,訪問的url資源,伺服器返回的http狀態碼等重要資訊。一條典型的web訪問日誌如下 規劃 1 要解決問題 2 nignx沒有自動分開檔案儲存日誌的機制。由於nginx它不會幫你自動分檔...

Nginx日誌切割

編輯指令碼tianshl tianshl nginx vim nginx log division.sh指令碼內容 bin sh 昨天日期 yesterday date v 1d y m d 日誌目錄 log path usr local var log nginx sdk日誌路徑 sdk path...

Nginx日誌切割

由於 nginx 的日誌都是寫在乙個檔案當中的,因此,我們需要每天零點將前一天的日誌存為另外乙個檔案,這裡我們就將 nginx 位於 logs 目錄中的 access.log 存為 access yyyy mm dd log 的檔案。其實 logs 目錄中還有個 error.log 的錯誤日誌檔案,...