nginx系列之三:日誌配置

**

前言

**

nginx系列之一:nginx入門
nginx系列之二:配置文件解讀
nginx系列之三:日誌配置
nginx系列之四:web服務器
nginx系列之五: 負載均衡
nginx系列之六:cache服務
nginx系列之七:限流配置
nginx系列之八:使用upsync模塊實現負載均衡

轉自:在此感謝原博主的整理分享

一、nginx access日誌配置

1.1 access_log日誌配置

access_log用來定義日誌級別,日誌位置。語法如下:
日誌級別: debug > info > notice > warn > error > crit > alert > emerg

語法格式: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;

默認值 : access_log logs/access.log combined;
作用域 : http, server, location, if in location, limit_except

實例一:

access_log /var/logs/nginx-access.log compression buffer=32k;
  • 1

1.2 log_format 定義日誌格式

語法格式: log_format name [escape=default|json] string …;
默認值 : log_format combined “…”;
作用域 : http

實例一:

log_format compression '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $bytes_sent '
                       '"$http_referer" "$http_user_agent" "$gzip_ratio"';
access_log /var/logs/nginx-access.log compression buffer=32k;
  • 1
  • 2
  • 3
  • 4

1.3 open_log_file_cache

使用open_log_file_cache來設置日誌文件緩存(默認是off)。

  • max:設置緩存中的最大文件描述符數量,如果緩存被佔滿,採用LRU算法將描述符關閉。
  • inactive:設置存活時間,默認是10s
  • min_uses:設置在inactive時間段內,日誌文件最少使用多少次後,該日誌文件描述符記入緩存中,默認是1次
  • valid:設置檢查頻率,默認60s
  • off:禁用緩存

語法格式: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
默認值: open_log_file_cache off;
作用域: http, server, location

實例一

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
  • 1

1.4 日誌中常用的全局變量

  • $remote_addr, $http_x_forwarded_for 記錄客戶端IP地址
  • $remote_user記錄客戶端用戶名稱
  • $request記錄請求的URL和HTTP協議(GET,POST,DEL,等)
  • $status記錄請求狀態
  • $body_bytes_sent發送給客戶端的字節數,不包括響應頭的大小; 該變量與Apache模塊mod_log_config裏的“%B”參數兼容。
  • $bytes_sent發送給客戶端的總字節數。
  • $connection連接的序列號。
  • $connection_requests 當前通過一個連接獲得的請求數量。
  • $msec 日誌寫入時間。單位爲秒,精度是毫秒。
  • $pipe如果請求是通過HTTP流水線(pipelined)發送,pipe值爲“p”,否則爲“.”。
  • $http_referer 記錄從哪個頁面鏈接訪問過來的
  • $http_user_agent記錄客戶端瀏覽器相關信息
  • $request_length請求的長度(包括請求行,請求頭和請求正文)。
  • $request_time 請求處理時間,單位爲秒,精度毫秒; 從讀入客戶端的第一個字節開始,直到把最後一個字符發送給客戶端後進行日誌寫入爲止。
  • $time_iso8601 ISO8601標準格式下的本地時間。
  • $time_local通用日誌格式下的本地時間。

二、nginx日誌調試技巧

2.1 僅記錄固定 IP 的錯誤

當你設置日誌級別成 debug,如果你在調試一個在線的高流量網站的話,你的錯誤日誌可能會記錄每個請求的很多消息,這樣會變得毫無意義。

events{...}中配置如下內容,可以使 Nginx 記錄僅僅來自於你的 IP 的錯誤日誌。

events {
        debug_connection 1.2.3.4;
}
  • 1
  • 2
  • 3

2.2 調試 nginx rewrite 規則

調試rewrite規則時,如果規則寫錯只會看見一個404頁面,可以在配置文件中開啓nginx rewrite日誌,進行調試。

server {
        error_log    /var/logs/nginx/example.com.error.log;
        rewrite_log on;
}
  • 1
  • 2
  • 3
  • 4

rewrite_log on; 開啓後,它將發送所有的 rewrite 相關的日誌信息到 error_log 文件中,使用 [notice] 級別。隨後就可以在error_log 查看rewrite信息了。

2.3 使用location記錄指定URL的日誌

server {
        error_log    /var/logs/nginx/example.com.error.log;
        location /static/ {
        error_log /var/logs/nginx/static-error.log debug; 
    }         
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

配置以上配置後,/static/ 相關的日誌會被單獨記錄在static-error.log文件中。

nginx日誌共三個參數

  1. access_log: 定義日誌的路徑及格式。
  2. log_format: 定義日誌的模板。
  3. open_log_file_cache: 定義日誌文件緩存。

proxy_set_header X-Forwarded-For :如果後端Web服務器上的程序需要獲取用戶IP,從該Header頭獲取。proxy_set_header X-Forwarded-For $remote_addr;

三、常用例子

3.1 main格式

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                       '$upstream_addr $upstream_response_time $request_time ';
access_log  logs/access.log  main;
  • 1
  • 2
  • 3
  • 4
  • 5

3.2 json格式

log_format logstash_json '{"@timestamp":"$time_iso8601",'
       '"host": "$server_addr",'
       '"client": "$remote_addr",'
       '"size": $body_bytes_sent,'
       '"responsetime": $request_time,'
       '"domain": "$host",'
       '"url":"$request_uri",'
       '"referer": "$http_referer",'
       '"agent": "$http_user_agent",'
       '"status":"$status",'
       '"x_forwarded_for":"$http_x_forwarded_for"}';
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

坑點:
使用$uri 可以在nginx對URL進行更改或重寫,但是用於日誌輸出可以使用$request_uri代替,如無特殊業務需求,完全可以替換。

3.3 壓縮格式

日誌中增加了壓縮的信息。

http {
    log_format compression '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $body_bytes_sent '
                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';
    server {
        gzip on;
        access_log /spool/logs/nginx-access.log compression;
        ...
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.4 upstream格式

增加upstream消耗的時間。

http {
    log_format upstream_time '$remote_addr - $remote_user [$time_local] '
                             '"$request" $status $body_bytes_sent '
                             '"$http_referer" "$http_user_agent"'
                             'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
server {
    access_log /spool/logs/nginx-access.log upstream_time;
    ...
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

四、附錄:參考文檔

4.1 統計status 出現的次數

awk '{print $9}' access.log | sort | uniq -c | sort -rn

36461 200
483 500
87 404
9 400
3 302
1 499
1 403
1 301

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4.2 顯示返回302狀態碼的URL。

awk '($9 ~ /302/)' access.log | awk '{print $7}' | sort | uniq -c | sort -rn
1 /wp-login.php
1 /wp-admin/plugins.php?action=activate&plugin=ewww-image-optimizer%2Fewww-image-optimizer.php&_wpnonce=cc4a379131
1 /wp-admin/
  • 1
  • 2
  • 3
  • 4

參考文檔

How to Configure Custom Access and Error Log Formats in Nginx
如何在Nginx中配置自定義訪問和錯誤日​​志格式

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