上線了一個小的預約程序,配置通過nginx進行訪問入口,默認的日志是沒有請求時間的,因此需要配置一下,將每一次的請求的訪問響應時間記錄出來,備查與優化使用.
一、默認的日志格式
默認的日志格式如下(默認是注解掉的,系統也自動會使用):
????#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;
二、我使用的日志格式
我僅僅在默認的基礎上加上響應時間的兩個我較關心的參數:request_time與upstream_response_time
將以下的配置開放并修改(我后面用了格式2,時間在前面,容易查看):
帶時間數據參數的日志格式1
????log_format??main??'$remote_addr?-?$remote_user?[$time_local]?"$request"?' ??????????????????????'$status?$body_bytes_sent?"$http_referer"?' ??????????????????????'"$http_user_agent"?"$http_x_forwarded_for"?"$request_time"?"$upstream_response_time"'; ????access_log??logs/access.log??main;
調整了下時間參數的顯示順序的格式2:
????log_format??main??'$remote_addr?-?$remote_user?[$request_time?$upstream_response_time]?[$time_local]?"$request"?' ??????????????????????'$status?$body_bytes_sent?"$http_referer"?' ??????????????????????'"$http_user_agent"?"$http_x_forwarded_for"'; ????access_log??logs/access.log??main;
注意的是:log_format與access_log的注釋都要放開,僅放開log_format也是不生效的.
#設置成格式2后,可以用相應的正則表達式,查看大于1秒的日志,分兩步如下:
##1.高亮時間數據的正則表達式 tail?-f?access.log?|grep?"[[0-9].[0-9][0-9][0-9]?[0-9].[0-9][0-9][0-9]]" ##2.大于1秒的日志的正則表達式,即將第一個數字改成[1-9]即可 tail?-f?access.log?|grep?"[[1-9].[0-9][0-9][0-9]?[0-9].[0-9][0-9][0-9]]"
三、參數
說明
-
$remote_addr:客戶端地址
-
$remote_user:客戶端用戶名稱?
-
$time_local:訪問時間和時區
-
$request:請求的URI和HTTP協議
-
$status:HTTP請求狀態
-
$body_bytes_sent:發送給客戶端文件內容大小
-
$http_referer:url跳轉來源
-
$http_user_agent:用戶終端瀏覽器等信息
-
$http_host:請求地址,即瀏覽器中你輸入的地址(IP或域名)
-
$request_time:處理請求的總時間,包含了用戶數據接收時間
-
$upstream_response_time:建立連接和從上游服務器接收響應主體的最后一個字節之間的時間
-
$upstream_connect_time:花費在與上游服務器建立連接上的時間
-
$upstream_header_time:建立連接和從上游服務器接收響應頭的第一個字節之間的時間
四、測試效果
修改前默認日志
127.0.0.1 – – [03/May/2022:12:02:51 +0800] “GET /byhsyyfront/byPages/ HTTP/1.1” 304 0 “-” “Mozilla/5.0 (windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) chrome/99.0.4844.51 safari/537.36″127.0.0.1 – – [03/May/2022:12:02:51 +0800] “GET /byhsyyGateway/byhsyySystem/verifyCode/getVerifyCode HTTP/1.1” 200 2553 “http://localhost:8881/byhsyyfront/byPages/” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36”
修改后日志
127.0.0.1 – – [03/May/2022:12:00:47 +0800] “GET /byhsyyfront/byPages/ HTTP/1.1” 304 0 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36” “-” “0.025” “0.025”
127.0.0.1 – – [03/May/2022:12:00:47 +0800] “GET /byhsyyGateway/byhsyySystem/verifyCode/getVerifyCode HTTP/1.1” 200 2178 “http://localhost:8881/byhsyyfront/byPages/” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36” “-” “0.037” “0.037”
可以看到修改后的最后多了兩個關于時間的參數數據,可以用于響應時間快慢分析.