Nginx訪問日誌 日誌切割 靜態檔案管理

2021-12-30 10:59:47 字數 4212 閱讀 2393

nginx日誌格式:

' "$http_referer" "$http_user_agent"';說明:

「combined_ realip」:日誌格式名稱;'$remote_ addr $http_ x_ forwarded_ for [$time_ local]' ' $host "$request_uri" $status' ' "$http_ referer" "$http_ user_ agent"' :日誌內容。

注釋:

名稱含義

$remote_addr

客戶端ip(公網ip)

**伺服器的ip

$time_local

伺服器本地時間

$host

訪問主機名(網域名稱)

$request_uri

訪問的url位址

$status

狀態碼

定義虛擬主機的前提是在nginx配置檔案中設定日誌格式,然後才能在虛擬主機中進行呼叫(格式名稱)。

[root@123 ~]# cd /usr/local/nginx/conf/vhost/

[root@123 vhost]# ls

aaa.com.conf test.com.conf

定義test.com.conf日誌格式:

[root@123 vhost]# vim test.com.conf

…… access_log /tmp/test.com.log combined_realip;

#指定日誌位置及格式

檢查錯誤:

[root@123 vhost]# /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@123 vhost]# curl -x127.0.0.1:80 test.com

this is test.com

[root@123 vhost]# cat /tmp/test.com.log

127.0.0.1 - [11/aug/2017:15:09:54 +0800] test.com "/" 200 "-" "curl/7.29.0"因為nginx沒有自帶的日誌切割工具,所以需要借助系統日誌切割命令或使用日誌切割指令碼。

為了方便管理,shell指令碼統一儲存位置:/usr/local/sbin/

[root@123 vhost]# vim /usr/local/sbin/nginx_log_rotate.sh

#! /bin/bash

d=`date -d "-1 day" +%y%m%d`

#定義切割時間(切割一天前的日誌)

logdir="/tmp/"

#此處指定要切割的日誌路徑(該路徑來自虛擬主機配置檔案)

nginx_pid="/usr/local/nginx/logs/nginx.pid"

#呼叫pid的目的是執行命令:/bin/kill -hup `cat $nginx_pid`

#該命令等價於命令:nginx -s reload(重新載入檔案),確保與虛擬主機配置檔案變更保持同步

#該位址來自nginx配置檔案

cd $logdir

for log in `ls *.log`

do mv $log $log-$d

done

#此處使用通配進行迴圈,對所有復合條件的日誌檔案進行切割

/bin/kill -hup `cat $nginx_pid`

#執行此命令進行過載生成新的日誌檔案來記錄新的日誌執行該指令碼:

[root@123 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh

++ date -d '-1 day' +%y%m%d

+ d=20170810

+ logdir=/tmp/

+ nginx_pid=/usr/local/nginx/logs/nginx.pid

+ cd /tmp/

++ ls test.com.log yum.log

+ for log in '`ls *.log`'

+ mv test.com.log test.com.log-20170810

+ for log in '`ls *.log`'

+ mv yum.log yum.log-20170810

++ cat /usr/local/nginx/logs/nginx.pid

+ /bin/kill -hup 1251說明:?-x選項的作用是顯示指令碼執行過程。

注:?該指令碼配合任務計畫cron使用,定期進行切割和清理。

核心配置引數:

#匹配檔案型別

location ~ .*\.(js|css)$

access_log /tmp/test.com.log combined_realip;

#指定日誌位置及格式檢測:

[root@123 vhost]# /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@123 vhost]# /usr/local/nginx/sbin/nginx -s reload

訪問index.html:

[root@123 vhost]# !curl

curl -x127.0.0.1:80 test.com

this is test.com

[root@123 vhost]# !cat

cat /tmp/test.com.log

127.0.0.1 - [11/aug/2017:17:45:28 +0800] test.com "/" 200 "-" "curl/7.29.0"

即:有日誌!

訪問baidu.png檔案:

說明:max-age=604800s=7天,即該檔案快取的過期時間為7天!

[root@123 test.com]# cat /tmp/test.com.log

127.0.0.1 - [11/aug/2017:17:45:28 +0800] test.com "/" 200 "-" "curl/7.29.0"

即:無該檔案的訪問日誌!!!

NGINX訪問日誌和日誌切割

當我們訪問nginx服務時,nginx會記錄日誌,nginx日誌分兩種,一種是訪問日誌,一種是錯誤日誌,訪問日誌記錄在 access.log 檔案中,錯誤日誌記錄在 error.log 檔案中 編譯安裝了nginx,預設情況下,access.log日誌會放在nginx安裝路徑的logs目錄中 roo...

Nginx訪問日誌 日誌切割 靜態檔案不記日誌

十二周三次課 1月4日 12.10 nginx訪問日誌 在主配置檔案中定義格式,在虛擬主機中 server 定義日誌路徑 vim usr local nginx conf nginx.conf 搜尋log format 在虛擬主機中 server 定義日誌路徑 vim usr local nginx...

nginx自動切割訪問日誌

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