Linux – Nginx log 格式

Loading

Linux 下常用的 Web (http) server 為 Apache ,我們還可以選擇 Nginx (音同 Engine X) 來架設.

關於 Nginx 請參考

測試平台為 CentOS 7 x86_64 虛擬機

NginX 的 log 檔案 access.log 與 error.log 存儲位置,以及 log 格式 (format) 皆是定義在 /etc/nginx/nginx.conf

error_log  /var/log/nginx/error.log warn;
access_log  /var/log/nginx/access.log main;

error_log 預設使用 warn ,用以紀錄 warn 等級以上的錯誤訊息,其他可以使用的錯誤等級 debug , info, notice , warn, error crit, alert 以及 emerg .
access.log 後面的 main 代表要使用 log_format 的 main 紀錄格式.

    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

實際來看一下 nginx 的 access.log 紀錄.

[root@localhost ~]# cat /var/log/nginx/access.log
192.168.95.1 - - [18/Jul/2018:20:38:23 -0400] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0" "-"

HTTP Client 的 IP

$remote_addr -
192.168.95.1 -

HTTP Client 的使用者與存取時間.

$remote_user [$time_local] 
- [18/Jul/2018:20:38:23 -0400]

存取方式

"$request" 
"GET / HTTP/1.1" 

狀態 1xx 是訊息 , 2xx 是成功 , 3xx 是重新導向 , 4xx 是用戶端錯誤 以及 5xx 是伺服器錯誤,詳細代碼請參考 https://zh.wikipedia.org/zh-tw/HTTP%E7%8A%B6%E6%80%81%E7%A0%81

'$status 
200

傳送資料 bytes 為單位.

$body_bytes_sent 
612   
"$http_referer" '
"-"
'"$http_user_agent" 
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0"
"$http_x_forwarded_for"';
"-"

其他可用參數,詳細參數請參考 http://nginx.org/en/docs/http/ngx_http_log_module.html
$upstream_connect_time – The time spent on establishing a connection with an upstream server
$upstream_header_time – The time between establishing a connection and receiving the first byte of the response header from the upstream server
$upstream_response_time – The time between establishing a connection and receiving the last byte of the response body from the upstream server
$request_time – The total time spent processing a request

    log_format upstream_time '$remote_addr - $remote_user [$time_local] '
                             '"$request" $status $body_bytes_sent '
                             '"$http_referer" "$http_user_agent"'
                             'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';

error_log

實際來看一下 nginx 的 access.log 紀錄.

[root@localhost ~]# cat /var/log/nginx/error.log 
2018/07/18 20:34:40 [error] 1796#1796: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.95.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.95.152"

access.log 我找不到格式定義,直接看其實蠻清楚各所代表的意思.

2018/07/18 20:34:40 

這一行可能會看不懂,它所代表的意思為 [錯誤等級] Process ID# Thread ID: *Connect ID

[error] 1796#1796: *1 

透過 ps -aux 可以觀察目前 nginx 的 PID 為何.

nginx      1796  0.0  0.2  48892  2244 ?        S    20:34   0:00 nginx: worker process
open() "/usr/share/nginx/html/favicon.ico" 
failed (2: No such file or directory)
client: 192.168.95.1
server: localhost
request: "GET /favicon.ico HTTP/1.1"
host: "192.168.95.152"

更多關於 Nginx log 請參考 – https://docs.nginx.com/nginx/admin-guide/monitoring/logging/

沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料