1.修改配置,在http{}中添加html
log_format access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"url":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"status":"$status"}'; access_log /var/log/nginx/access.log access_json;
2.重啓nginx
systemctl restart nginx
或者web
nginx -s reload
3.訪問,輸出日誌json
{"@timestamp":"2019-10-12T18:41:48+08:00","host":"127.0.0.1","clientip":"127.0.0.1","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"localhost","url":"/index.html","domain":"localhost","xff":"-","referer":"-","status":"304"}
日誌中變量的含義瀏覽器
log_format access_json '{"ts":"$fmt_localtime",' '"server_addr":"$server_addr",' '"request":"$request",' '"http_accept_language":"$http_accept_language",' '"http_user_agent":"$http_user_agent",' '"remote_addr":"$remote_addr",' '"body_bytes_sent,":$body_bytes_sent,' '"request_time,":$request_time,' '"request_length":$request_length,' '"http_host":"$http_host",' '"url":"$uri",' '"host":"$host",' '"http_x_forwarded_for":"$http_x_forwarded_for",' '"http_referer":"$http_referer"' '"status":"$status"}';
好比bash
{ "ts":"2019-10-14 16:02:19", "server_addr":"127.0.0.1", "request":"GET /index.html HTTP/1.1", "http_accept_language":"zh-CN,en-US;q=0.7,en;q=0.3", "http_user_agent":"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0", "remote_addr":"127.0.0.1", "body_bytes_sent,":0, "request_time,":0.000, "request_length":448, "http_host":"localhost", "url":"/index.html", "host":"localhost", "http_x_forwarded_for":"-", "http_referer":"-""status":"304" }
參考服務器
https://www.iteye.com/blog/bit1129-2205848
參數含義dom
1.訪問時間 ts
2.訪問端口 server_addr
3.請求方式(GET或者POST等)request
4.用戶瀏覽器語言。如:上例中的 "es-ES,es;q=0.8" http_accept_language
5.用戶瀏覽器其餘信息,瀏覽器版本、瀏覽器類型等 http_user_agent
6.客戶端(用戶)IP地址 remote_addr
7.發送給客戶端的文件主體內容的大小 body_bytes_sent
8.整個請求的總時間 request_time
9.請求的長度(包括請求行,請求頭和請求正文)request_length
10.請求的url地址(目標url地址)的host http_host
11.請求url地址(去除host部分) uri
12.host 與 http_host的區別在於當使用非80/443端口的時候,http_host = host:port host
13.客戶端的真實ip,一般web服務器放在反向代理的後面,這樣就不能獲取到客戶的IP地址了,通 過$remote_add拿到的IP地址是反向代理服務器的iP地址。反向代理服務器在轉發請求的http頭信息中,能夠增長 x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務器地址 http_x_forwarded_for
14.記錄從哪一個頁面連接訪問過來的(請求頭Referer的內容)http_referer
15.請求狀態(狀態碼,200表示成功) statusurl