一、 錯誤日誌
vim nginx.conf
error_log /tmp/error.log error;
語法:
Syntax: error_log file [level];
Default:
error_log logs/error.log error;
Context: main, http, mail, stream, server, location
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
補充說明:
錯誤日誌的,默認情況下不指定也沒有關係,因爲nginx很少有錯誤日誌記錄的。
但有時出現問題時,是有必要記錄一下錯誤日誌的,方便我們排查問題。
error_log 級別分爲 debug, info, notice, warn, error, crit 默認爲crit
該級別在日誌名後邊定義格式如下:
error_log /your/path/error.log crit;
crit 記錄的日誌最少,而debug記錄的日誌最多。
如果nginx遇到一些問題,比如502比較頻繁出現,但是看默認的error_log並沒有看到有意義的信息,
那麼就可以調一下錯誤日誌的級別,當你調成error級別時,錯誤日誌記錄的內容會更加豐富
二、訪問日誌(重點關注)
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' --- 定義日誌信息要記錄的內容格式
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main; --- 調用定義格式信息,生成訪問日誌
變量詳解:(官方鏈接:http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log)
$remote_addr 10.0.0.1 --- 訪問客戶端的源地址信息
$remote_user - --- 訪問客戶端認證用戶信息 ???
[$time_local] --- 顯示訪問時間
$request GET / HTTP/1.1 --- 請求行信息
$status 304 --- 狀態碼信息(304狀態碼利用緩存顯示頁面信息)
$body_bytes_sent --- 服務端響應客戶端的數據大小信息
$http_referer --- 記錄鏈接到網站的域名信息 ???
$ http_user_agent --- 用戶訪問網站客戶端軟件標識信息
用戶利用客戶端瀏覽器測試訪問時,win10默認瀏覽器會有異常問
$http_x_forwarded_for --- ??? 反向代理
三、日誌要進行切割
-
利用shell腳本實現日誌切割
[root@web01 scripts]# vim cut_log.sh
#!/bin/bash
data_info=$(date +%F-%H:%M)mv /application/nginx/logs/www_access.log /application/nginx/logs/access.log.$data_info
/application/nginx/sbin/nginx -s reload - 定時任務 cut nginx log cron
. ./6 . . . /bin/sh /server/scripts/cut_log.sh &>/dev/null