Nginx訪問日志access_log配置及信息詳解(推薦)
Nginx訪問日志access_log配置及信息
Nginx訪問日志主要有兩個(gè)參數(shù)控制:
log_format #用來定義記錄日志的格式(可以定義多種日志格式,取不同名字即可)
access_log #用來指定日至文件的路徑及使用的何種日志格式記錄日志
# 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的默認(rèn)值:
#access_log logs/access.log main;
log_format log_format語法格式及參數(shù)語法說明如下:
log_format <NAME> <Strin---g>; 關(guān)鍵字 格式標(biāo)簽 日志格式 關(guān)鍵字:其中關(guān)鍵字error_log不能改變 格式標(biāo)簽:格式標(biāo)簽是給一套日志格式設(shè)置一個(gè)獨(dú)特的名字 日志格式:給日志設(shè)置格式 作用域 : http
eg:
log_format compression '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"'; access_log /spool/logs/nginx-access.log compression buffer=32k;
log_format格式變量:
參數(shù) 說明 示例 $remote_addr 客戶端地址 211.28.65.253 $remote_user 客戶端用戶名稱 -- $time_local 訪問時(shí)間和時(shí)區(qū) 18/Jul/2012:17:00:01 +0800 $request 請求的URI和HTTP協(xié)議 "GET /article-10000.html HTTP/1.1" $http_host 請求地址,即瀏覽器中你輸入的地址(IP或域名) www.wang.com 192.168.100.100 $status HTTP請求狀態(tài) 200 $upstream_status upstream狀態(tài) 200 $body_bytes_sent 發(fā)送給客戶端文件內(nèi)容大小 1547 $http_referer url跳轉(zhuǎn)來源 https://www.baidu.com/ $http_user_agent 用戶終端瀏覽器等信息 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C; $ssl_protocol SSL協(xié)議版本 TLSv1 $ssl_cipher 交換數(shù)據(jù)中的算法 RC4-SHA $upstream_addr 后臺(tái)upstream的地址,即真正提供服務(wù)的主機(jī)地址 10.10.10.100:80 $request_time 整個(gè)請求的總時(shí)間 0.205 $upstream_response_time 請求過程中,upstream響應(yīng)時(shí)間 0.002
access_log
語法格式及參數(shù)語法說明如下:
access_log <FILE> <NAME>; 關(guān)鍵字 日志文件 格式標(biāo)簽 關(guān)鍵字:其中關(guān)鍵字error_log不能改變 日志文件:可以指定任意存放日志的目錄 格式標(biāo)簽:給日志文件套用指定的日志格式 其他語法: access_log off; #關(guān)閉access_log,即不記錄訪問日志 access_log path [format [buffer=size [flush=time]] [if=condition]]; access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition]; access_log syslog:server=address[,parameter=value] [format [if=condition]]; 說明: buffer=size #為存放訪問日志的緩沖區(qū)大小 flush=time #為緩沖區(qū)的日志刷到磁盤的時(shí)間 gzip[=level] #表示壓縮級別 [if = condition] #表示其他條件 作用域(參數(shù)的標(biāo)簽段位置) : http, server, location, if in location, limit_except
eg:
access_log /spool/logs/nginx-access.log compression buffer=32k;
open_log_file_cache
使用open_log_file_cache來設(shè)置日志文件緩存(默認(rèn)是off)。
max:設(shè)置緩存中的最大文件描述符數(shù)量,如果緩存被占滿,采用LRU算法將描述符關(guān)閉。
inactive:設(shè)置存活時(shí)間,默認(rèn)是10s
min_uses:設(shè)置在inactive時(shí)間段內(nèi),日志文件最少使用多少次后,該日志文件描述符記入緩存中,默認(rèn)是1次
valid:設(shè)置檢查頻率,默認(rèn)60s
off:禁用緩存
語法格式: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off; 默認(rèn)值: open_log_file_cache off; 作用域: http, server, location
eg:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
nginx日志調(diào)試技巧
設(shè)置 Nginx 僅記錄來自于你的 IP 的錯(cuò)誤
當(dāng)你設(shè)置日志級別成 debug,如果你在調(diào)試一個(gè)在線的高流量網(wǎng)站的話,你的錯(cuò)誤日志可能會(huì)記錄每個(gè)請求的很多消息,這樣會(huì)變得毫無意義。
在events{…}中配置如下內(nèi)容,可以使 Nginx 記錄僅僅來自于你的 IP 的錯(cuò)誤日志。
events { debug_connection 1.2.3.4; }
調(diào)試 nginx rewrite 規(guī)則
調(diào)試rewrite規(guī)則時(shí),如果規(guī)則寫錯(cuò)只會(huì)看見一個(gè)404頁面,可以在配置文件中開啟nginx rewrite日志,進(jìn)行調(diào)試。
server { error_log /var/logs/nginx/example.com.error.log; rewrite_log on; }
rewrite_log on; 開啟后,它將發(fā)送所有的 rewrite 相關(guān)的日志信息到 error_log 文件中,使用 [notice] 級別。隨后就可以在error_log 查看rewrite信息了。
使用location記錄指定URL的日志
server { error_log /var/logs/nginx/example.com.error.log; location /static/ { error_log /var/logs/nginx/static-error.log debug; } }
Nginx配置訪問日志過程:
(1)創(chuàng)建log_format語句
worker_processes 1; error_log logs/error.log error; events { worker_connections 1024; } http { include status.conf; include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; 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; server { listen 80; server_name localhost; rewrite ^/.* http://www.wl.com permanent; } include vhost/*.conf; }
(2)插入access_log語句
server { access_log /data/log/www; listen 80; server_name abc.com www.wl.com; location / { root /data/www/www; index index.html index.htm; } error_log logs/error_www.wl.com.log error; access_log logs/access_www.wl.com.log main; #新增內(nèi)容↑ }
(3)重啟服務(wù)
nginx -t nginx -s reload
常用例子
main格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' '$upstream_addr $upstream_response_time $request_time '; access_log logs/access.log main;
json格式
log_format logstash_json '{"@timestamp":"$time_iso8601",' '"host": "$server_addr",' '"client": "$remote_addr",' '"size": $body_bytes_sent,' '"responsetime": $request_time,' '"domain": "$host",' '"url":"$request_uri",' '"referer": "$http_referer",' '"agent": "$http_user_agent",' '"status":"$status",' '"x_forwarded_for":"$http_x_forwarded_for"}';
解釋:
uri請求中的當(dāng)前uri(不帶請求參數(shù),參數(shù)位于uri請求中的當(dāng)前URI(不帶請求參數(shù),參數(shù)位于 uri請求中的當(dāng)前URI(不帶請求參數(shù),參數(shù)位于args),不同于瀏覽器傳遞的$request_uri的值,它可以通過內(nèi)部重定向,或者使用index指令進(jìn)行修改。不包括協(xié)議和主機(jī)名,例如/foo/bar.html。
requesturi這個(gè)變量等于包含一些客戶端請求參數(shù)的原始uri,它無法修改,請查看request—_uri這個(gè)變量等于包含一些客戶端請求參數(shù)的原始URI,它無法修改,請查看 requestu?ri這個(gè)變量等于包含一些客戶端請求參數(shù)的原始URI,它無法修改,請查看uri更改或重寫URI。
也就是說:requesturi是原始請求url,request_uri是原始請求URL, requestu?ri是原始請求URL,uri則是經(jīng)過nginx處理請求后剔除參數(shù)的URL,所以會(huì)將漢字表現(xiàn)為union。
坑點(diǎn):
使用uri可以在nginx對url進(jìn)行更改或重寫,但是用于日志輸出可以使用uri可以在nginx對URL進(jìn)行更改或重寫,但是用于日志輸出可以使用 uri可以在nginx對URL進(jìn)行更改或重寫,但是用于日志輸出可以使用request_uri代替,如無特殊業(yè)務(wù)需求,完全可以替換。
壓縮格式
日志中增加了壓縮的信息。
http { log_format compression '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"'; server { gzip on; access_log /spool/logs/nginx-access.log compression; ... } }
upstream格式
增加upstream消耗的時(shí)間。
http { 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"'; server { access_log /spool/logs/nginx-access.log upstream_time; ... } }
統(tǒng)計(jì)信息
統(tǒng)計(jì)status 出現(xiàn)的次數(shù)
awk '{print $9}' access.log | sort | uniq -c | sort -rn 36461 200 483 500 87 404 9 400 3 302 1 499 1 403 1 301
顯示返回302狀態(tài)碼的URL。
awk '($9 ~ /302/)' access.log | awk '{print $7}' | sort | uniq -c | sort -rn 1 /wp-login.php 1 /wp-admin/plugins.php?action=activate&plugin=ewww-image-optimizer%2Fewww-image-optimizer.php&_wpnonce=cc4a379131 1 /wp-admin/
到此這篇關(guān)于Nginx訪問日志access_log配置及信息詳解的文章就介紹到這了,更多相關(guān)Nginx訪問日志access_log內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使Nginx服務(wù)器支持中文URL的相關(guān)配置詳解
這篇文章主要介紹了使Nginx服務(wù)器支持中文URL的相關(guān)配置方法,搜索引擎方面Google目前對中文URL的支持度也很好,需要的朋友可以參考下2016-01-01使用nginx實(shí)現(xiàn)一個(gè)端口和ip訪問多個(gè)vue前端的全過程
為滿足單端口訪問多個(gè)前端應(yīng)用的需求,需要對nginx進(jìn)行配置,同時(shí)修改vue項(xiàng)目的publicPath參數(shù),這篇文章主要介紹了使用nginx實(shí)現(xiàn)一個(gè)端口和ip訪問多個(gè)vue前端的相關(guān)資料,需要的朋友可以參考下2024-09-09nginx實(shí)現(xiàn)一個(gè)域名配置多個(gè)laravel項(xiàng)目的方法示例
這篇文章主要介紹了nginx實(shí)現(xiàn)一個(gè)域名配置多個(gè)laravel項(xiàng)目的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01nginx中g(shù)zip_types匹配content-type的方式
這篇文章主要介紹了nginx中g(shù)zip_types匹配content-type的方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Ubuntu使用nginx搭建webdav文件服務(wù)器的詳細(xì)過程
今天通過本文給大家分享Ubuntu使用nginx搭建webdav文件服務(wù)器的詳細(xì)過程,在這小編提示大家在安裝nginx時(shí)需要先安裝nginx-full,具體安裝方法跟隨小編一起通過本文學(xué)習(xí)下吧2021-05-05Nginx?生產(chǎn)環(huán)境安全配置加固的實(shí)現(xiàn)
本文主要介紹了Nginx?生產(chǎn)環(huán)境安全配置加固的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03使用Nginx為自己的網(wǎng)站資源加上防盜鏈保護(hù)實(shí)現(xiàn)
這篇文章主要為大家介紹了使用Nginx為自己的網(wǎng)站資源加上防盜鏈保護(hù)實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08