Operations 13 min read

Analyzing Nginx Access Logs for Traffic, Performance, and Optimization

This article explains how to extract valuable performance and traffic insights from Nginx access logs using shell commands and awk, covering request volume, peak rates, bandwidth usage, slow‑query detection, URL normalization, and practical optimization recommendations for web operations.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Analyzing Nginx Access Logs for Traffic, Performance, and Optimization

Nginx (Engine‑X) is a widely used load balancer and reverse proxy that generates massive log files, often hundreds of megabytes to tens of gigabytes per day, which are rarely inspected before logrotate deletes them.

The log format is highly customizable and, with fields like $time_local , can serve as a file‑based time‑series database, offering a hidden treasure of operational data.

Request Access Analysis

Each log line represents a single request. Simple shell commands can reveal total request count, average requests per second, and peak RPS, providing a quick view of system load. Example commands:

less main.log | wc -l
less main.log | awk '{sec=substr($4,2,20);reqs++;reqsBySec[sec]++;} END{print reqs/length(reqsBySec)}'
less main.log | awk '{sec=substr($4,2,20);requests[sec]++;} END{for(s in requests){printf("%s %s\n", requests[s],s)}}' | sort -nr | head -n 3

These metrics help gauge system pressure and guide scaling or performance testing decisions.

Traffic Rate Analysis

Beyond request counts, logs often contain response size and time, enabling calculation of bandwidth and throughput. Example to list top static resources by bandwidth:

less static.log | awk 'url=$7; requests[url]++;bytes[url]+=$10} END{for(url in requests){printf("%sMB %sKB/req %s %s\n", bytes[url]/1024/1024, bytes[url]/requests[url]/1024, requests[url], url)}}' | sort -nr | head -n 15

Static assets typically dominate bandwidth, highlighting compression or CDN opportunities.

Slow Query Analysis

Analogous to database slow‑query logs, Nginx’s $request_time and $upstream_response_time fields allow identification of slow requests. Steps include:

Calculate the proportion of requests exceeding a latency threshold (e.g., 2 s):

less main.log | awk -v limit=2 '{min=substr($4,2,17);reqs[min]++;if($11>limit){slowReqs[min]++}} END{for(m in slowReqs){printf("%s %s%% %s %s\n", m, slowReqs[m]/reqs[m]*100, slowReqs[m], reqs[m])}}' | more

Compare upstream response time vs. Nginx processing time to spot problematic backend servers.

less main.log | awk '{upServer=$13;upTime=$12;if(upServer=="-"){upServer="Nginx"};if(upTime=="-"){upTime=0};upTimes[upServer]+=upTime;count[upServer]++;totalCount++;} END{for(server in upTimes){printf("%s %s%% %ss %s\n", count[server], count[server]/totalCount*100, upTimes[server]/count[server], server)}}' | sort -nr

Inspect per‑second bandwidth to detect peak usage:

less main.log | awk '{second=substr($4,2,20);bytes[second]+=$10;} END{for(s in bytes){printf("%sKB %s\n", bytes[s]/1024, s)}}' | more

These analyses help determine whether issues stem from user networks, backend bottlenecks, or bandwidth saturation.

Additional Optimization Ideas

Log user‑agent strings to monitor crawler activity, normalize URLs (remove parameters, extensions, and numeric IDs) for cleaner statistics, and track conversion rates across days to guide marketing timing.

less main.log | egrep 'spider|bot' | awk '{name=$17;if(index($15,"spider")>0){name=$15};spiders[name]++} END{for(name in spiders){printf("%s %s\n",spiders[name], name)}}' | sort -nr

Standardizing log formats, consistently recording timestamps, and capturing identifiers (user, request, application) are essential best practices.

Finally, various tools—from simple shell scripts to ELK stacks or language‑specific parsers (Python, Go, Lua)—can automate these analyses, turning raw Nginx logs into actionable insights.

backendoperationsPerformance MonitoringNginxlog analysisshell scriptingawk
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.