Nginx日誌格式設置

一、訪問日誌

nginx服務器日誌相關指令主要有兩條,一條是log_format,用來設置日誌格式,另外一條是access_log,用來指定日誌文件的存放路徑、格式和緩存大小,一般在nginx的配置文件中日記配置(/usr/local/nginx/conf/nginx.conf)。

nginx的log_format有很多可選的參數用於指示服務器的活動狀態,默認的是:

log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
            '"$http_user_agent" "$http_x_forwarded_for"';

想要記錄更詳細的信息需要自己設置log_format,具體可設置的參數格式及說明如下:

參數說明示例
$remote_addr客戶端地址211.28.65.253
$remote_user客戶端用戶名稱
$time_local訪問時間和時區18/Jul/2012:17:00:01 +0800
$request請求的URI和HTTP協議“GET /article-10000.html HTTP/1.1”
$http_host請求地址,即瀏覽器中你輸入的地址(IP或域名)www.it300.com/192.168.100.100
$statusHTTP請求狀態200
$upstream_statusupstream狀態200
$body_bytes_sent發送給客戶端文件內容大小1547
$http_refererurl跳轉來源https://www.baidu.com/
$http_user_agent用戶終端瀏覽器等信息“Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;
$ssl_protocolSSL協議版本TLSv1
$ssl_cipher交換數據中的算法RC4-SHA
$upstream_addr後臺upstream的地址,即真正提供服務的主機地址10.10.10.100:80
$request_time整個請求的總時間0.205
$upstream_response_time請求過程中,upstream響應時間0.002

舉例說明如下: 
1、配置文件

#vim /usr/local/nginx/conf/nginx.conf
log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" $http_x_forwarded_for '
    '"$upstream_addr" "$upstream_status" "$upstream_response_time" "$request_time"';
    include /usr/local/nginx/conf/vhost/*.conf;

2、vhost中配置文件

#vim /usr/local/nginx/conf/vhost/web.confserver
 {
  listen 80 default;
  server_name www.it300.com;
  index index.html index.htm index.php;
  root /data/httpd/it300.com;
  location ~ .*\.php?$
  {
   include fastcgi.conf;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
  }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  {
  expires 30d;
  }
  location ~ .*\.(js|css)?$
  {
  expires 1h;
  }
  access_log /data/logs/it300.com.log access;
 }


二、錯誤日誌

錯誤日誌主要記錄客戶端訪問Nginx出錯時的日誌格式不支持自定義。通過錯誤日誌你可以得到系統某個服務或server的性能瓶頸等。因此將日誌好好利用你可以得到很多有價值的信息。

錯誤日誌由指令error_log來指定具體格式如下
        error_log path(存放路徑) level(日誌等級)


        path含義同access_loglevel表示日誌等級具體如下
        [ debug | info | notice | warn | error | crit ]


        從左至右日誌詳細程度逐級遞減即debug最詳細crit最少。


        舉例說明如下
        error_log  logs/error.log  info;
        需要注意的是error_log off並不能關閉錯誤日誌而是會將錯誤日誌記錄到一個文件名爲off的文件中。


        正確的關閉錯誤日誌記錄功能的方法如下
        error_log /dev/null;
        上面表示將存儲日誌的路徑設置爲“垃圾桶”。



-- 2018-06-13 新增 --

nginx源碼提供兩種形式的時間格式

1、[$time_local]
展現形式:[13/Jun/2018:13:50:48 +0800]

2、[$time_iso8601]
展現形式:[2018-06-13T13:50:26+08:00]




本文轉自:http://www.it300.com/article-15353.htmlhttps://blog.csdn.net/u013474436/article/details/51317099



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