With the development of Internet technology, Web servers have become more and more powerful, and Nginx is one of the most popular Web servers. As a high-performance web server, Nginx provides rich functions and flexible configuration. One of the important functions is to record HTTP request logs. These logs record the details of all HTTP requests entering the Nginx server, which is very important for network administrators and can help them analyze website traffic, detect attacks and failures, etc.
The HTTP request log in Nginx is turned off by default and needs to be enabled manually. In the Nginx configuration file, you can use the access_log directive to enable and configure the HTTP request log. The following is the syntax of a typical access_log directive:
access_log /path/to/access.log combined;
/path/to/access.log is the path to the log file, and combined is The log format is specified. Nginx provides a variety of formats, including common, combined, main, referer, user_agent, etc.
The common format records basic information such as the user's IP address, access time, request method, request URL and HTTP version. The specific format is as follows:
$remote_addr - $remote_user [$time_local] “$request” $status $body_bytes_sent “$http_referer” “$http_user_agent”
The combined format includes all common formats information, as well as more detailed information such as the user's browser type, operating system and connection speed. The specific format is as follows:
$remote_addr - $remote_user [$time_local] “$request” $status $body_bytes_sent “$http_referer” “$http_user_agent” “$http_x_forwarded_for”
Understand Nginx’s HTTP The format of the request logs, and the next step is how to analyze these logs. Commonly used log analysis tools include AWStats, Webalizer and GoAccess. Here we take GoAccess as an example to introduce how to use it to analyze Nginx's HTTP request logs.
GoAccess is an open source real-time Web log analysis tool that supports logs in multiple formats and can generate interactive reports. First, you need to download and install it. You can download the latest version from the official website of GoAccess, or you can use the package manager to install it on a Linux system.
After the installation is complete, enter the following command in the terminal to use GoAccess to analyze Nginx's HTTP request log:
goaccess /path/to/access.log -o output.html -- log-format=COMBINED
Among them, /path/to/access.log is the path of Nginx’s HTTP request log file, -o specifies the name of the output file, and –log-format specifies the format of the log file.
After executing the above command, GoAccess will automatically start analyzing Nginx's HTTP request log and output a real-time report on the terminal. The report includes information such as the number of visits, time distribution, traffic proportion, the most popular pages and resources, the user's browser, operating system and IP address. Among them, the most commonly used indicators are the number of visits and the proportion of traffic. They can help administrators understand the visit situation and traffic distribution of the website, and then formulate corresponding optimization plans.
In addition to terminal output, GoAccess also supports generating reports in HTML, JSON and CSV formats. Just use the -o parameter to specify the output file name. For example:
goaccess /path/to/access.log -o output.html --log-format=COMBINED
This command will generate an HTML format report named output.html .
In general, using Nginx’s HTTP request log analysis tool can help administrators quickly understand website access and traffic distribution. Through in-depth analysis of these indicators, administrators can further optimize the performance and operational efficiency of the website. At the same time, you can also use log analysis tools to detect abnormal IPs, defend against attacks, locate faults, etc. Therefore, learning how to analyze Nginx's HTTP request logs is an essential skill for network administrators.
The above is the detailed content of HTTP request log analysis in Nginx. For more information, please follow other related articles on the PHP Chinese website!