1、訪問日誌php
nginx服務器日誌相關指令主要有兩條,一條是log_format
,用來設置日誌格式,另一條是access_log
,用來指定日誌文件的存放路徑、格式和緩存大小,通常在nginx的配置文件中日記配置(/usr/local/nginx/conf/nginx.conf
)。css
nginx的log_format
有不少可選的參數用於指示服務器的活動狀態,默認的是:html
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,具體可設置的參數格式及說明以下:
nginx
參數 | 說明 | 示例 |
$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 |
舉例說明以下:
一、配置文件web
#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中配置文件算法
#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; }
2、錯誤日誌vim
錯誤日誌主要記錄客戶端訪問Nginx出錯時的日誌格式不支持自定義。經過錯誤日誌你能夠獲得系統某個服務或server的性能瓶頸等。所以將日誌好好利用你能夠獲得不少有價值的信息。瀏覽器
錯誤日誌由指令error_log來指定具體格式以下
error_log path(存放路徑) level(日誌等級)緩存
path含義同access_loglevel表示日誌等級具體以下
[ debug | info | notice | warn | error | crit ]bash
從左至右日誌詳細程度逐級遞減即debug最詳細crit最少。
舉例說明以下
error_log logs/error.log info;
須要注意的是error_log off並不能關閉錯誤日誌而是會將錯誤日誌記錄到一個文件名爲off的文件中。
正確的關閉錯誤日誌記錄功能的方法以下
error_log /dev/null;
上面表示將存儲日誌的路徑設置爲「垃圾桶」。
-- 2018-06-13 新增 --
nginx源碼提供兩種形式的時間格式
一、[$time_local] 展示形式:[13/Jun/2018:13:50:48 +0800] 二、[$time_iso8601] 展示形式:[2018-06-13T13:50:26+08:00]
本文轉自:http://www.it300.com/article-15353.html、https://blog.csdn.net/u013474436/article/details/51317099