Nginx 日志配置

日志使用用來進(jìn)行數(shù)據(jù)統(tǒng)計(jì)、問題排錯(cuò)的重要手段。本文主要介紹 nginx 日志相關(guān)的配置如 access_log、log_format、log_not_found、rewrite_log、error_log 以及 Nginx 日志切割。

日志相關(guān)配置

nginx日志相關(guān)涉及的配置有:
access_log:訪問日志;
log_format:日志格式;
rewrite_log:重定向日志;
error_log:錯(cuò)誤日志;
nginx 具備非常靈活的日志記錄模式,每個(gè)級別的配置可以有各自獨(dú)立的訪問日志。日志格式通過 log_format 命令來定義。

access_log 配置

語法:

  • access_log path [format [buffer=size [flush=time]]];
  • access_log path format gzip[=level] [buffer=size] [flush=time];
  • access_log syslog:server=address[,parameter=value] [format];
  • access_log off; #不記錄日志

默認(rèn)值: access_log logs/access.log combined;
使用默認(rèn) combined 格式記錄日志:access_log logs/access.log 或 access_log logs/access.log combined;
配置段: http, server, location, if in location, limit_except
參數(shù)解釋:

  • gzip:壓縮等級。
  • buffer:設(shè)置內(nèi)存緩存區(qū)大小。
  • flush:保存在緩存區(qū)中的最長時(shí)間。

log_format配置

語法:log_format name string ……;
默認(rèn)值: log_format combined "……";
配置段: http

釋義:name 表示格式名稱,string 表示等義的格式。log_format 有一個(gè)默認(rèn)的無需設(shè)置的 combined 日志格式,相當(dāng)于 apache 的 combined 日志格式。

示例1:

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

示例2:

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

配置相關(guān)變量釋義:

$remote_addr:表示客戶端地址;
$remote_user:表示http客戶端請求Nginx認(rèn)證的用戶名;
$time_local:Nginx通用日志格式下的本地時(shí)間;
$request:request請求行,請求的URL、GET等方法、HTTP協(xié)議版本;
$request_length:請求的長度;
$request_time:請求處理時(shí)間,單位為秒,精度為毫秒;
$status:response返回狀態(tài)碼;
$body_bytes_sent:發(fā)送給客戶端的字節(jié)數(shù),不包括響應(yīng)頭的大小,即服務(wù)端響應(yīng)給客戶端body信息大?。?br /> $http_referer:http上一級頁面,即從哪個(gè)頁面鏈接訪問過來的,用于防盜鏈、用戶行為分析;
$http_user_agent:http頭部信息,記錄客戶端瀏覽器相關(guān)信息;
$connection:連接的序列號;
$connection_requesta:當(dāng)前通常一個(gè)連接獲得的請求數(shù)量;
$msec:日志寫入時(shí)間,單位為秒,精度為毫秒;
$pipe:如果請求是通過HTTP流水線(pipelined)發(fā)送,pipe值為‘p’,否則為“.”;
$http_x_forwarded_for:http請求攜帶的http信息。
提示:如果nginx位于負(fù)載均衡器,squid,nginx反向代理之后,web服務(wù)器無法直接獲取到客戶端真實(shí)的IP地址了。 $remote_addr獲取反向代理的IP地址。反向代理服務(wù)器在轉(zhuǎn)發(fā)請求的http頭信息中,可以增加X-Forwarded-For信息,用來記錄客戶端IP地址和客戶端請求的服務(wù)器地址。



rewrite_log配置

語法: rewrite_log on | off;
默認(rèn)值:rewrite_log off;
配置段:http,server,location,if
作用:由ngx_http_rewrite_module模塊提供的。用來記錄重寫日志的,對于調(diào)試重寫規(guī)則建議開啟。啟用時(shí)將在error log中記錄notice級別的重寫日志。

error_log配置

語法:error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];
默認(rèn)值:error_log logs/error.log error;
配置段:main,http,server,location
作用:配置錯(cuò)誤日志。


丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清