Nginx的日志log_format

1.HTTP請求
大家都知道nginx作為webserver和http的代理,處理的就是http請求。
http請求是是建立在tcp基礎上的。

一個完整的http請求包括request和response兩部分。
request組成:請求行,請求頭,請求數據
response組成:狀態行,消息報頭,響應正文

我們可以在命令行查看一個完整的http請求:

 curl -v https://coding.imooc.com > /dev/null

> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: coding.imooc.com
> Accept: */*
> 
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0< HTTP/1.1 200 OK
< Server: nginx
< Date: Mon, 22 Jan 2018 13:38:31 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< Vary: Accept-Encoding
< Set-Cookie: imooc_uuid=373f05d8-21b6-4c34-af8a-5c3c452cbece; expires=Tue, 22-Jan-2019 13:38:30 GMT; path=/; domain=.imooc.com
< Set-Cookie: imooc_isnew=1; expires=Tue, 22-Jan-2019 13:38:30 GMT; path=/; domain=.imooc.com
< Set-Cookie: imooc_isnew_ct=1516628310; expires=Tue, 22-Jan-2019 13:38:30 GMT; path=/; domain=.imooc.com
< Set-Cookie: cvde=5a65e956d1de1-1; path=/; domain=.imooc.com
< 
{ [data not shown]
100 75022    0 75022    0     0  67577      0 --:--:--  0:00:01 --:--:-- 67648
* Connection #0 to host coding.imooc.com left intact

大于號部分是請求部分,小于號部分是響應部分

2.Nginx日志類型
包括:error.log access_log
error.log 主要是處理http請求錯誤和nginx本身服務錯誤狀態,按照不同的錯誤級別記錄
access_log 主要是記錄處理每次http請求訪問的狀態

日志主要實現方式是使用 log_format

nginx記錄的每次信息都可以當做一個變量,log_format就是將這些變量組合起來,記錄到日志中去

我們看一下log_format的配置

Syntax: log_format name [escape=default|json] string …;
Default: log_format combined "...";
Context: http (約束log_format的配置位置)

我們查看一個配置實例


圖片.png

Nginx大致有三類變量能夠記錄在log_format 中

  • HTTP請求變量- arg_PARAMETER http_HEADER send_http_HEADER(服務端返回)
  • 內置變量- Nginx內置的
  • 自定義變量- 自己定義的

例子一:獲取到request的請求頭信息
首先我們編輯配置文件log_format 部分


圖片.png

在保存編輯之后檢查配置文件的正確性

[root@hongshaorou nginx]# nginx -t -c /etc/nginx/nginx.conf 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

-t 表示檢查正確性
-c 執行目錄

檢查完成后重新加載服務

[root@hongshaorou nginx]# nginx -s reload -c /etc/nginx/nginx.conf 

然后我們多次請求本機,查看日志輸出

[root@hongshaorou nginx]# curl 127.0.0.1

[root@hongshaorou nginx]# tail -n 2 /var/log/nginx/access.log
curl/7.29.0127.0.0.1 - - [24/Jan/2018:20:36:45 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
curl/7.29.0127.0.0.1 - - [24/Jan/2018:20:36:51 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

我們看到已經記錄了anget信息。

對于nginx的內置變量,我們可以查看官網信息:
https://nginx.org/en/docs/http/ngx_http_core_module.html#var_status

我們看一下默認的log_format

    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  /var/log/nginx/access.log  main;

我們看到默認的格式,基本都是單引號 '' 包裹著一些變量,還包括 中劃線 - 方括號 [] 作為分隔符一起打印。
每個變量都有這含義:

remote_addr:對應客戶端的地址
remote_user:是請求客戶端請求認證的用戶名,如果沒有開啟認證模塊的話是值為空。
time_local:表示nginx服務器時間
request:表示request請求頭的行
status:表示response的返回狀態
body_bytes_sent:表示從服務端返回給客戶端的body數據大小
http_referer:表示請求的上一級頁面
http_user_agent:表示agent信息
http_x_forwarded_for:會記錄每一級請求中信息
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容