Nginx access.log日志詳解及統(tǒng)計(jì)分析小結(jié)
一、nginx的access.log
1.日志文件一般存放在 /var/log/nginx 下,若是docker啟動(dòng)則可以使用主機(jī)掛載位置,直接使用 tail -f命令即可查看access日志。
2.access.log具體每項(xiàng)的含義:
參數(shù) 說明 示例
$remote_addr 客戶端地址 172.17.0.1
$remote_user 客戶端用戶名稱 --
$time_local 訪問時(shí)間和時(shí)區(qū) [29/Dec/2022:10:17:14 +0000]
$request 請(qǐng)求的URI和HTTP協(xié)議 "GET /test/nginx/proxy HTTP/1.1"
$http_host 請(qǐng)求地址,即瀏覽器中你輸入的地址(IP或域名) 10.1.7.33
$status HTTP請(qǐng)求狀態(tài) 200
$upstream_status upstream狀態(tài) 200
$body_bytes_sent 發(fā)送給客戶端文件內(nèi)容大小 38
$http_referer url跳轉(zhuǎn)來源 -
$http_user_agent 用戶終端瀏覽器等信息 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
$http_cookie 用戶cookie信息 "grafana_session=73d13d456cb4363f8a48f5501348669e"
$ssl_protocol SSL協(xié)議版本 TLSv1
$ssl_cipher 交換數(shù)據(jù)中的算法 RC4-SHA
$upstream_addr 后臺(tái)upstream的地址,即真正提供服務(wù)的主機(jī)地址 "10.1.7.33:8102"
$request_time 整個(gè)請(qǐng)求的總時(shí)間 0.012
$upstream_response_time 請(qǐng)求過程中,upstream響應(yīng)時(shí)間 0.012
3.access.log 的格式是可以自己自定義,輸出的信息格式在nginx.conf中設(shè)置

可以在location中增加header,輸出用戶代理服務(wù)器地址
location /test/ {
#limit_req zone=allips burst=1 nodelay;
proxy_pass http://myServer/test/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
#代理服務(wù)器地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 8m;
}
二、日志流量統(tǒng)計(jì)
Nginx: PV、UV、獨(dú)立IP做網(wǎng)站的都知道,平常經(jīng)常要查詢下網(wǎng)站PV、UV等網(wǎng)站的訪問數(shù)據(jù),當(dāng)然如果網(wǎng)站做了CDN的話,nginx本地的日志就沒什么意義了,下面就對(duì)nginx網(wǎng)站的日志訪問數(shù)據(jù)做下統(tǒng)計(jì);
- UV(Unique Visitor):獨(dú)立訪客,將每個(gè)獨(dú)立上網(wǎng)電腦(以cookie為依據(jù))視為一位訪客,一天之內(nèi)(00:00-24:00),訪問您網(wǎng)站的訪客數(shù)量。一天之內(nèi)相同cookie的訪問只被計(jì)算1次
- PV(Page View):訪問量,即頁面瀏覽量或者點(diǎn)擊量,用戶每次對(duì)網(wǎng)站的訪問均被記錄1次。用戶對(duì)同一頁面的多次訪問,訪問量值累計(jì)
- 統(tǒng)計(jì)獨(dú)立IP:00:00-24:00內(nèi)相同IP地址只被計(jì)算一次,做網(wǎng)站優(yōu)化的朋友最關(guān)心這個(gè)
日志統(tǒng)計(jì)分析,日志內(nèi)容如下:
root@DESKTOP-0NVFL1I:/home/volumes/nginx_vts/log# tail -f access.log 172.17.0.1 - [30/Dec/2022:02:17:19 +0000] "GET /test/nginx/proxy HTTP/1.1" "10.1.7.33" 200 200 38 "-" "grafana_session=73d13d456cb4363f8a48f5501348669e" "-" "10.1.7.33:8101" 0.008 0.008 172.17.0.1 - [30/Dec/2022:02:17:20 +0000] "GET /test/nginx/proxy HTTP/1.1" "10.1.7.33" 200 200 38 "-" "grafana_session=73d13d456cb4363f8a48f5501348669e" "-" "10.1.7.33:8102" 0.016 0.016 172.17.0.1 - [30/Dec/2022:02:19:55 +0000] "GET /test/nginx/proxy HTTP/1.1" "10.1.7.33" 200 200 38 "-" "grafana_session=73d13d456cb4363f8a48f5501348669e" "-" "10.1.7.33:8101" 0.010 0.010 172.17.0.1 - [30/Dec/2022:02:19:56 +0000] "GET /test/nginx/proxy HTTP/1.1" "10.1.7.33" 200 200 38 "-" "grafana_session=73d13d456cb4363f8a48f5501348669e" "-" "10.1.7.33:8102" 0.011 0.011
統(tǒng)計(jì)接口地址訪問量
grep /test access.log | wc -l
PV統(tǒng)計(jì)
awk '{print $6}' access.log | wc -l
UV統(tǒng)計(jì)
awk '{print $13}' access.log | sort -r |uniq -c |wc -l
獨(dú)立IP統(tǒng)計(jì)
awk '{print $1}' access.log | sort -r |uniq -c | wc -l
三、配置access.log按天生成
1.nginx.conf配置文件http代碼塊中修改成如下代碼

#配置按天生成access.log日志文件
map $time_iso8601 $logdate {
'~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
default 'date-not-found';
}
#access_log /var/log/nginx/access.log main;
access_log /var/log/nginx/access-$logdate.log main;
error_log /var/log/nginx/error.log;
2.重啟nginx,再次訪問接口,并查看日志,日志按天生成
root@DESKTOP-0NVFL1I:/home/volumes/nginx_vts/log# ll -rwxrwxrwx 1 buckletime buckletime 744 Dec 30 11:14 access-2022-12-30.log -rwxrwxrwx 1 buckletime buckletime 744 Dec 30 10:19 access.log -rw-r--r-- 1 root root 12586 Dec 30 10:14 error.log
若權(quán)限不夠則,修改日志文件權(quán)限
chmod -R 777 /var/log/nginx/
四、nginx.conf配置
附上完整的nginx.conf配置
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
#開啟nginx監(jiān)控模塊
vhost_traffic_status_zone;
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr $remote_user [$time_local] "$request" '
'"$http_host" $status $upstream_status '
'$body_bytes_sent "$http_referer" '
'"$http_cookie" "$http_x_forwarded_for" '
'"$upstream_addr" $request_time $upstream_response_time';
#配置按天生成日志文件
map $time_iso8601 $logdate {
'~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
default 'date-not-found';
}
#access_log /var/log/nginx/access.log main;
access_log /var/log/nginx/access-$logdate.log main;
error_log /var/log/nginx/error.log;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#include /etc/nginx/conf.d/*.conf;
upstream myServer{
server 10.1.7.33:8101;
server 10.1.7.33:8102;
}
server {
listen 80;
server_name 10.1.7.33;
root /usr/share/nginx/html;
location /test/ {
#limit_req zone=allips burst=1 nodelay;
proxy_pass http://myServer/test/;
proxy_set_header Host $host;
#用戶的真實(shí)ip
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Cookie $http_cookie;
#用戶的真實(shí)ip和經(jīng)過的每一層代理服務(wù)器的ip
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 8m;
}
#nginx狀態(tài)監(jiān)控接口
#location /status {
# vhost_traffic_status_display;
# vhost_traffic_status_display_format html;
#}
}
}到此這篇關(guān)于Nginx access.log日志詳解及統(tǒng)計(jì)分析小結(jié)的文章就介紹到這了,更多相關(guān)Nginx access.log日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
寶塔nginx部署前端頁面刷新報(bào)404錯(cuò)誤解決辦法
使用nginx部署前端項(xiàng)目是一篇非常詳細(xì)的教程,旨在幫助初學(xué)者使用Nginx來部署前端項(xiàng)目,這篇文章主要給大家介紹了關(guān)于寶塔nginx部署前端頁面刷新報(bào)404錯(cuò)誤的解決辦法,需要的朋友可以參考下2024-03-03
nginx實(shí)現(xiàn)動(dòng)靜分離的示例代碼
這篇文章主要介紹了nginx實(shí)現(xiàn)動(dòng)靜分離的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Nginx HTTP 配置指令的實(shí)現(xiàn)示例
本文主要介紹了Nginx的配置文件結(jié)構(gòu)和HTTP配置指令,涵蓋了從請(qǐng)求處理到安全、日志、負(fù)載均衡、緩存等各個(gè)方面,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12
Nginx進(jìn)階配置實(shí)現(xiàn)SSL證書部署與資源防盜鏈實(shí)操
本文主要介紹了在Web服務(wù)運(yùn)維中使用Nginx進(jìn)行SSL證書部署,實(shí)現(xiàn)HTTPS加密訪問,強(qiáng)制HTTP轉(zhuǎn)HTTPS以及配置Nginx防盜鏈保護(hù)靜態(tài)資源安全的具體方法,感興趣的可以了解一下2026-04-04

