Nginx日誌格式設置

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

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

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,具體可設置的參數格式及說明以下:html

參數 說明 示例
$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
$status HTTP請求狀態 200
$upstream_status upstream狀態 200
$body_bytes_sent 發送給客戶端文件內容大小 1547
$http_referer url跳轉來源 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_protocol SSL協議版本 TLSv1
$ssl_cipher 交換數據中的算法 RC4-SHA
$upstream_addr 後臺upstream的地址,即真正提供服務的主機地址 10.10.10.100:80
$request_time 整個請求的總時間 0.205
$upstream_response_time 請求過程當中,upstream響應時間 0.002

舉例說明以下: 
一、配置文件python

#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;

二、vhost中配置文件linux

#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;
 }

 

-- 2018-06-13 新增 --nginx

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

一、[$time_local]

展示形式:[13/Jun/2018:13:50:48 +0800]


二、[$time_iso8601]

展示形式:[2018-06-13T13:50:26+08:00]

 

-- 2018-06-28 新增 --算法

一、access_logvim

access_log 指令用來指定日誌文件的存放路徑包含日誌文件名、格式和緩存大小具體以下瀏覽器

access_log   path(存放路徑)  [format(自定義日誌格式名稱)  [buffer=size | off]]

舉例說明以下

access_log  logs/access.log  main;

若是想關閉日誌能夠以下

access_log off;

可以使用 access_log 指令的字段包括 http、server、location。

須要注意的是 Nginx 進程設置的用戶和組必須對日誌路徑有建立文件的權限不然會報錯。

小技巧

若是須要在 access_log 中記錄 post 請求的參數能夠參考 如何在access log中記錄post請求的參數 。

Nginx 支持爲每一個 location 指定強大的日誌記錄。

一樣的鏈接能夠在同一時間輸出到不止一個的日誌中更多信息請查看 Nginx模塊參考手冊:日誌模塊(Log) 。

 

二、錯誤日誌

錯誤日誌主要記錄客戶端訪問 Nginx 出錯時的日誌格式不支持自定義。經過錯誤日誌你能夠獲得系統某個服務或 server 的性能瓶頸等。

所以將日誌好好利用你能夠獲得不少有價值的信息。錯誤日誌由指令 error_log 來指定具體格式以下

error_log   path(存放路徑)   level(日誌等級)

path 含義同 access_log level 表示日誌等級具體以下

[ debug | info | notice | warn | error | crit ]

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

舉例說明以下

error_log  logs/error.log  info;

須要注意的是 error_log  off 並不能關閉錯誤日誌而是會將錯誤日誌記錄到一個文件名爲 off 的文件中。

正確的關閉錯誤日誌記錄功能的方法以下

error_log  /dev/null;

# 上面表示將存儲日誌的路徑設置爲「垃圾桶」

 

 

參考資料

1. Nginx 日誌各項參數含義

2. 如何在access log中記錄post請求的參數

3. Nginx模塊參考手冊:日誌模塊(Log) 

相關文章
相關標籤/搜索