Nginx is a high-performance open-source web server and reverse proxy server widely used in large-scale website architectures. To ensure the stable operation of a system and optimize performance, it is essential to record and analyze web service request logs. This article will detail how to implement request log recording and analysis through Nginx proxy servers and how to analyze logs using commonly used tools.
First, we need to enable request log functionality in the Nginx configuration file. Open the Nginx configuration file `/etc/nginx/nginx.conf` and add the following configuration within the http block:
http { ... log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; ... }
The above configuration defines a log format called `main` using the `log_format` directive. This format includes common request information, such as client IP, request time, request method, and status code. The `access_log` directive then writes the log information to the specified file `/var/log/nginx/access.log`.
Next, we need to configure Nginx’s reverse proxy functionality to forward requests to the backend web service. Open the Nginx configuration file and add the following configuration within the server block:
server { ... location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; ... } ... }
In this configuration, `location /` means all requests are forwarded to the backend web service (e.g., `http://backend`). The `proxy_set_header` directive is used to set request headers like Host and X-Real-IP to ensure that the backend server receives the correct request information.
To analyze the request logs, we can use command-line tools such as `awk`, `grep`, and `sort` to process the logs. Here’s a simple example that counts the number of visits from different IPs within a specific time range:
$ awk -v from="2022-01-01 00:00:00" -v to="2022-01-01 23:59:59" \ '$4 >= from && $4 <= to {print $1}' /var/log/nginx/access.log | \ sort | uniq -c | sort -rn
In this command, `awk` filters the log entries based on the time range and extracts the IP addresses. Then, the `sort` command sorts the IP addresses, `uniq -c` counts the number of visits per IP, and `sort -rn` sorts the results in descending order by visit count.
By following the above steps, we can successfully implement web request log recording and analysis through the Nginx proxy server. With proper configuration of Nginx's log recording and reverse proxy features and the use of log analysis tools, we can effectively monitor the performance of web services and optimize them. We hope that the techniques outlined in this article will help you master Nginx log configuration and analysis, thus improving your web service management efficiency.