Nginx訪問(wèn)日志access_log配置及信息詳解(推薦)
Nginx訪問(wèn)日志access_log配置及信息
Nginx訪問(wèn)日志主要有兩個(gè)參數(shù)控制:
log_format #用來(lái)定義記錄日志的格式(可以定義多種日志格式,取不同名字即可)
access_log #用來(lái)指定日至文件的路徑及使用的何種日志格式記錄日志
# 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語(yǔ)法格式及參數(shù)語(yǔ)法說(shuō)明如下:
log_format <NAME> <Strin---g>;
關(guān)鍵字 格式標(biāo)簽 日志格式
關(guān)鍵字:其中關(guān)鍵字error_log不能改變
格式標(biāo)簽:格式標(biāo)簽是給一套日志格式設(shè)置一個(gè)獨(dú)特的名字
日志格式:給日志設(shè)置格式
作用域 : httpeg:
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ù) 說(shuō)明 示例 $remote_addr 客戶端地址 211.28.65.253 $remote_user 客戶端用戶名稱 -- $time_local 訪問(wèn)時(shí)間和時(shí)區(qū) 18/Jul/2012:17:00:01 +0800 $request 請(qǐng)求的URI和HTTP協(xié)議 "GET /article-10000.html HTTP/1.1" $http_host 請(qǐng)求地址,即瀏覽器中你輸入的地址(IP或域名) www.wang.com 192.168.100.100 $status HTTP請(qǐng)求狀態(tài) 200 $upstream_status upstream狀態(tài) 200 $body_bytes_sent 發(fā)送給客戶端文件內(nèi)容大小 1547 $http_referer url跳轉(zhuǎn)來(lái)源 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è)請(qǐng)求的總時(shí)間 0.205 $upstream_response_time 請(qǐng)求過(guò)程中,upstream響應(yīng)時(shí)間 0.002
access_log
語(yǔ)法格式及參數(shù)語(yǔ)法說(shuō)明如下:
access_log <FILE> <NAME>;
關(guān)鍵字 日志文件 格式標(biāo)簽
關(guān)鍵字:其中關(guān)鍵字error_log不能改變
日志文件:可以指定任意存放日志的目錄
格式標(biāo)簽:給日志文件套用指定的日志格式
其他語(yǔ)法:
access_log off; #關(guān)閉access_log,即不記錄訪問(wèn)日志
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]];
說(shuō)明:
buffer=size #為存放訪問(wèn)日志的緩沖區(qū)大小
flush=time #為緩沖區(qū)的日志刷到磁盤(pán)的時(shí)間
gzip[=level] #表示壓縮級(jí)別
[if = condition] #表示其他條件
作用域(參數(shù)的標(biāo)簽段位置) : http, server, location, if in location, limit_excepteg:
access_log /spool/logs/nginx-access.log compression buffer=32k;
open_log_file_cache
使用open_log_file_cache來(lái)設(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:禁用緩存
語(yǔ)法格式: 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, locationeg:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
nginx日志調(diào)試技巧
設(shè)置 Nginx 僅記錄來(lái)自于你的 IP 的錯(cuò)誤
當(dāng)你設(shè)置日志級(jí)別成 debug,如果你在調(diào)試一個(gè)在線的高流量網(wǎng)站的話,你的錯(cuò)誤日志可能會(huì)記錄每個(gè)請(qǐng)求的很多消息,這樣會(huì)變得毫無(wú)意義。
在events{…}中配置如下內(nèi)容,可以使 Nginx 記錄僅僅來(lái)自于你的 IP 的錯(cuò)誤日志。
events {
debug_connection 1.2.3.4;
}調(diào)試 nginx rewrite 規(guī)則
調(diào)試rewrite規(guī)則時(shí),如果規(guī)則寫(xiě)錯(cuò)只會(huì)看見(jiàn)一個(gè)404頁(yè)面,可以在配置文件中開(kāi)啟nginx rewrite日志,進(jìn)行調(diào)試。
server {
error_log /var/logs/nginx/example.com.error.log;
rewrite_log on;
}rewrite_log on; 開(kāi)啟后,它將發(fā)送所有的 rewrite 相關(guān)的日志信息到 error_log 文件中,使用 [notice] 級(jí)別。隨后就可以在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配置訪問(wèn)日志過(guò)程:
(1)創(chuàng)建log_format語(yǔ)句
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語(yǔ)句
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請(qǐng)求中的當(dāng)前uri(不帶請(qǐng)求參數(shù),參數(shù)位于uri請(qǐng)求中的當(dāng)前URI(不帶請(qǐng)求參數(shù),參數(shù)位于 uri請(qǐng)求中的當(dāng)前URI(不帶請(qǐng)求參數(shù),參數(shù)位于args),不同于瀏覽器傳遞的$request_uri的值,它可以通過(guò)內(nèi)部重定向,或者使用index指令進(jìn)行修改。不包括協(xié)議和主機(jī)名,例如/foo/bar.html。
requesturi這個(gè)變量等于包含一些客戶端請(qǐng)求參數(shù)的原始uri,它無(wú)法修改,請(qǐng)查看request—_uri這個(gè)變量等于包含一些客戶端請(qǐng)求參數(shù)的原始URI,它無(wú)法修改,請(qǐng)查看 requestu?ri這個(gè)變量等于包含一些客戶端請(qǐng)求參數(shù)的原始URI,它無(wú)法修改,請(qǐng)查看uri更改或重寫(xiě)URI。
也就是說(shuō):requesturi是原始請(qǐng)求url,request_uri是原始請(qǐng)求URL, requestu?ri是原始請(qǐng)求URL,uri則是經(jīng)過(guò)nginx處理請(qǐng)求后剔除參數(shù)的URL,所以會(huì)將漢字表現(xiàn)為union。
坑點(diǎn):
使用uri可以在nginx對(duì)url進(jìn)行更改或重寫(xiě),但是用于日志輸出可以使用uri可以在nginx對(duì)URL進(jìn)行更改或重寫(xiě),但是用于日志輸出可以使用 uri可以在nginx對(duì)URL進(jìn)行更改或重寫(xiě),但是用于日志輸出可以使用request_uri代替,如無(wú)特殊業(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訪問(wèn)日志access_log配置及信息詳解的文章就介紹到這了,更多相關(guān)Nginx訪問(wèn)日志access_log內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使Nginx服務(wù)器支持中文URL的相關(guān)配置詳解
這篇文章主要介紹了使Nginx服務(wù)器支持中文URL的相關(guān)配置方法,搜索引擎方面Google目前對(duì)中文URL的支持度也很好,需要的朋友可以參考下2016-01-01
使用nginx實(shí)現(xiàn)一個(gè)端口和ip訪問(wèn)多個(gè)vue前端的全過(guò)程
為滿足單端口訪問(wèn)多個(gè)前端應(yīng)用的需求,需要對(duì)nginx進(jìn)行配置,同時(shí)修改vue項(xiàng)目的publicPath參數(shù),這篇文章主要介紹了使用nginx實(shí)現(xiàn)一個(gè)端口和ip訪問(wèn)多個(gè)vue前端的相關(guān)資料,需要的朋友可以參考下2024-09-09
nginx實(shí)現(xiàn)一個(gè)域名配置多個(gè)laravel項(xiàng)目的方法示例
這篇文章主要介紹了nginx實(shí)現(xiàn)一個(gè)域名配置多個(gè)laravel項(xiàng)目的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
nginx中g(shù)zip_types匹配content-type的方式
這篇文章主要介紹了nginx中g(shù)zip_types匹配content-type的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Ubuntu使用nginx搭建webdav文件服務(wù)器的詳細(xì)過(guò)程
今天通過(guò)本文給大家分享Ubuntu使用nginx搭建webdav文件服務(wù)器的詳細(xì)過(guò)程,在這小編提示大家在安裝nginx時(shí)需要先安裝nginx-full,具體安裝方法跟隨小編一起通過(guò)本文學(xué)習(xí)下吧2021-05-05
Nginx?生產(chǎn)環(huán)境安全配置加固的實(shí)現(xiàn)
本文主要介紹了Nginx?生產(chǎn)環(huán)境安全配置加固的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(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
簡(jiǎn)介Nginx中的location匹配規(guī)則
這篇文章主要介紹了簡(jiǎn)介Nginx中的location匹配規(guī)則,Nginx是一個(gè)高速的基于事務(wù)的非阻塞服務(wù)器,需要的朋友可以參考下2015-07-07

