Nginx is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP proxy server. Nginx is widely used in both high-traffic websites and small personal blogs. In actual production environments, the analysis of Nginx logs helps us understand the access situation of the website, find potential problems and optimize them. This article will implement a comprehensive statistical analysis of Nginx logs by writing a Shell script. Nginx log formatFirst, we need to make sure that the Nginx log format is similar to the following: Assume that our log file is called access.log. Shell script implementationNext, we write a Shell script to perform statistical analysis on Nginx logs. This script includes the following functions:
Script code implementation: 1. Count the number of various status codes: ① Arry[$12] += 1;:
② total++;:
③ for (s in Arry):
④ printf "%d\t%.4f\t%s\n", Arry[s], Arry[s] / total, s:
Running the above command will produce the following output: Count the number of various status codes 2. Count the most visited Referers After executing the above command, the output is as shown below: Count the most visited Referers 3. Count the most visited URIs After executing the above command, the output is as shown below: Count the most visited URIs 4. Count the most visited IP and User-Agent(1) Count the number of IP visits After executing the above command, the output is as shown below: Count the number of IP visits (2) Count the most visited User-Agents After executing the above command, the output is as shown below: Statistics visit Statistics visit 5. Statistics on the number of requests per minute, traffic, request time, status code, etc. After executing the above command, the following results are output: SummarizeThrough the above Shell script, we can quickly and comprehensively analyze Nginx logs to understand the website's access and performance. This not only helps us find potential problems, but also provides strong data support for subsequent optimization work. In actual applications, you can further expand and customize this script according to your needs. How to obtain the scriptThe above scripts have been uploaded to gitee. You can get them if you need them. The repository on gitee mainly shares some commonly used scripts in work. You can frok or watch the repository so that you can pay attention to updates in time. Script Repository Warehouse address: https://gitee.com/didiplus/script |
>>: 7.2 Our computer room is disconnected from the Internet! What should I do?
Ever since 5G has entered the homes of ordinary p...
[[393747]] When it comes to 5G, is your first rea...
AlphaVPS is a foreign hosting company founded in ...
According to foreign media reports, market resear...
Email hosting is one of the main services provide...
Currently, 5G standardization has been fully laun...
Mobile substation is a kind of factory-prefabrica...
[51CTO.com original article] Xiao Nie just return...
spinservers has released a promotion for March, o...
[51CTO.com original article] On July 21-22, 2017,...
Local Area Networks (LANs) have historically been...
Nowadays, watching dramas, movies, new shows, and...
ThomasHost domain name was registered in 2012, an...
From November 25th to 27th, Fanruan's 4th Sma...
[[412357]] Yesterday, the three major operators r...