Nginx access.log日志詳解及統(tǒng)計分析小結
一、nginx的access.log
1.日志文件一般存放在 /var/log/nginx 下,若是docker啟動則可以使用主機掛載位置,直接使用 tail -f命令即可查看access日志。
2.access.log具體每項的含義:
參數(shù) 說明 示例
$remote_addr 客戶端地址 172.17.0.1
$remote_user 客戶端用戶名稱 --
$time_local 訪問時間和時區(qū) [29/Dec/2022:10:17:14 +0000]
$request 請求的URI和HTTP協(xié)議 "GET /test/nginx/proxy HTTP/1.1"
$http_host 請求地址,即瀏覽器中你輸入的地址(IP或域名) 10.1.7.33
$status HTTP請求狀態(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 后臺upstream的地址,即真正提供服務的主機地址 "10.1.7.33:8102"
$request_time 整個請求的總時間 0.012
$upstream_response_time 請求過程中,upstream響應時間 0.012
3.access.log 的格式是可以自己自定義,輸出的信息格式在nginx.conf中設置
可以在location中增加header,輸出用戶代理服務器地址
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; #代理服務器地址 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 8m; }
二、日志流量統(tǒng)計
Nginx: PV、UV、獨立IP做網(wǎng)站的都知道,平常經(jīng)常要查詢下網(wǎng)站PV、UV等網(wǎng)站的訪問數(shù)據(jù),當然如果網(wǎng)站做了CDN的話,nginx本地的日志就沒什么意義了,下面就對nginx網(wǎng)站的日志訪問數(shù)據(jù)做下統(tǒng)計;
- UV(Unique Visitor):獨立訪客,將每個獨立上網(wǎng)電腦(以cookie為依據(jù))視為一位訪客,一天之內(nèi)(00:00-24:00),訪問您網(wǎng)站的訪客數(shù)量。一天之內(nèi)相同cookie的訪問只被計算1次
- PV(Page View):訪問量,即頁面瀏覽量或者點擊量,用戶每次對網(wǎng)站的訪問均被記錄1次。用戶對同一頁面的多次訪問,訪問量值累計
- 統(tǒng)計獨立IP:00:00-24:00內(nèi)相同IP地址只被計算一次,做網(wǎng)站優(yōu)化的朋友最關心這個
日志統(tǒng)計分析,日志內(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)計接口地址訪問量
grep /test access.log | wc -l
PV統(tǒng)計
awk '{print $6}' access.log | wc -l
UV統(tǒng)計
awk '{print $13}' access.log | sort -r |uniq -c |wc -l
獨立IP統(tǒng)計
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
若權限不夠則,修改日志文件權限
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; #用戶的真實ip proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header Cookie $http_cookie; #用戶的真實ip和經(jīng)過的每一層代理服務器的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; #} } }
到此這篇關于Nginx access.log日志詳解及統(tǒng)計分析小結的文章就介紹到這了,更多相關Nginx access.log日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決nginx啟動失敗(bind()?to?0.0.0.0:80?failed,An?attempt?was?
這篇文章主要介紹了解決nginx啟動失敗問題(bind()?to?0.0.0.0:80?failed,An?attempt?was?made?to?access?a?socket?in?...),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05詳解Nginx防盜鏈和Nginx訪問控制與Nginx解析php的配置
這篇文章主要介紹了詳解Nginx防盜鏈和Nginx訪問控制與Nginx解析php的配置的相關資料,這里提供實例幫助大家,學習理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08在Linux中查看Apache或Nginx服務狀態(tài)的詳細步驟
在Linux中,查看Apache或Nginx服務的狀態(tài)通常涉及到使用系統(tǒng)管理工具或特定于這些Web服務器的命令,以下是如何查看Apache和Nginx服務狀態(tài)的詳細步驟,需要的朋友可以參考下2024-03-03解決Nginx網(wǎng)關超時出現(xiàn)504 GATEWAY TIMEOUT的問題
這篇文章主要給大家介紹了如何解決Nginx網(wǎng)關超時出現(xiàn)504 GATEWAY TIMEOUT的問題,文章通過代碼示例和圖文結合介紹的非常詳細,有遇到相同問題的朋友可以參考閱讀本文2023-11-11nginx如何配置同一個端口轉(zhuǎn)發(fā)多個項目
這篇文章主要介紹了nginx如何配置同一個端口轉(zhuǎn)發(fā)多個項目問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01服務器的負載均衡nginx+tomcat實現(xiàn)動靜分離
這篇文章主要為大家介紹了服務器的負載均衡nginx+tomcat實現(xiàn)動靜分離的案例實施步驟以及環(huán)境詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03