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ù) 說(shuō)明 示例
$remote_addr 客戶端地址 172.17.0.1
$remote_user 客戶端用戶名稱 --
$time_local 訪問(wèn)時(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)來(lái)源 -
$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)求過(guò)程中,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)站的訪問(wèn)數(shù)據(jù),當(dāng)然如果網(wǎng)站做了CDN的話,nginx本地的日志就沒(méi)什么意義了,下面就對(duì)nginx網(wǎng)站的日志訪問(wèn)數(shù)據(jù)做下統(tǒng)計(jì);
- UV(Unique Visitor):獨(dú)立訪客,將每個(gè)獨(dú)立上網(wǎng)電腦(以cookie為依據(jù))視為一位訪客,一天之內(nèi)(00:00-24:00),訪問(wèn)您網(wǎng)站的訪客數(shù)量。一天之內(nèi)相同cookie的訪問(wèn)只被計(jì)算1次
- PV(Page View):訪問(wèn)量,即頁(yè)面瀏覽量或者點(diǎn)擊量,用戶每次對(duì)網(wǎng)站的訪問(wèn)均被記錄1次。用戶對(duì)同一頁(yè)面的多次訪問(wèn),訪問(wèn)量值累計(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ì)接口地址訪問(wèn)量
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,再次訪問(wèn)接口,并查看日志,日志按天生成
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 { #開(kāi)啟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)過(guò)的每一層代理服務(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啟動(dòng)失敗(bind()?to?0.0.0.0:80?failed,An?attempt?was?
這篇文章主要介紹了解決nginx啟動(dòng)失敗問(wèn)題(bind()?to?0.0.0.0:80?failed,An?attempt?was?made?to?access?a?socket?in?...),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05詳解Nginx防盜鏈和Nginx訪問(wèn)控制與Nginx解析php的配置
這篇文章主要介紹了詳解Nginx防盜鏈和Nginx訪問(wèn)控制與Nginx解析php的配置的相關(guān)資料,這里提供實(shí)例幫助大家,學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08在Linux中查看Apache或Nginx服務(wù)狀態(tài)的詳細(xì)步驟
在Linux中,查看Apache或Nginx服務(wù)的狀態(tài)通常涉及到使用系統(tǒng)管理工具或特定于這些Web服務(wù)器的命令,以下是如何查看Apache和Nginx服務(wù)狀態(tài)的詳細(xì)步驟,需要的朋友可以參考下2024-03-03解決Nginx網(wǎng)關(guān)超時(shí)出現(xiàn)504 GATEWAY TIMEOUT的問(wèn)題
這篇文章主要給大家介紹了如何解決Nginx網(wǎng)關(guān)超時(shí)出現(xiàn)504 GATEWAY TIMEOUT的問(wèn)題,文章通過(guò)代碼示例和圖文結(jié)合介紹的非常詳細(xì),有遇到相同問(wèn)題的朋友可以參考閱讀本文2023-11-11nginx如何配置同一個(gè)端口轉(zhuǎn)發(fā)多個(gè)項(xiàng)目
這篇文章主要介紹了nginx如何配置同一個(gè)端口轉(zhuǎn)發(fā)多個(gè)項(xiàng)目問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01服務(wù)器的負(fù)載均衡nginx+tomcat實(shí)現(xiàn)動(dòng)靜分離
這篇文章主要為大家介紹了服務(wù)器的負(fù)載均衡nginx+tomcat實(shí)現(xiàn)動(dòng)靜分離的案例實(shí)施步驟以及環(huán)境詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03