Logging is a critical part of managing a web server. It allows you to monitor traffic, debug issues, and gather insights into user behavior. This guide focuses on setting up HTTP logging in NGINX to capture detailed information about requests and responses.
How to configure reverse proxy for Oracle APEX URL using nginx - Detailed blog
Configuration of custom HTTP Logging
Step 1: Define a Custom Log Format
Edit the Main NGINX Configuration File: Open the NGINX configuration file for editing:
sudo nano /etc/nginx/yourdomain.conf
Add a Custom Log Format: In the
http
block, define a custom log format:http { log_format custom_log_format '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" "$http_user_agent"'; access_log /var/log/nginx/http_requests.log custom_log_format; }
$remote_addr: IP address of the client.
$remote_user: Authenticated user (if any).
$time_local: Local time of the request.
$request: The requested resource and HTTP method.
$status: HTTP status code of the response.
$body_bytes_sent: Size of the response body.
$http_referer: The referring URL (if any).
$http_user_agent: The user agent string of the client.
Step 2: Configure Access Logs for a Specific Server Block
Navigate to Your Server Block Configuration: Edit the server block file for your domain:
sudo nano /etc/nginx/yourdomain.conf
Specify the Access Log Location: Add the following line inside the server block:
server { access_log /var/log/nginx/http_requests.log custom_log_format; ... }
Save and Exit the File: Save the changes and exit the editor.
Step 3: Test and Reload NGINX
Test the Configuration: Verify that the syntax is correct:
sudo nginx -t
Reload NGINX: Apply the configuration changes:
sudo systemctl reload nginx
Step 4: Monitor HTTP Logs
View Logs in Real-Time: Use the
tail
command to monitor HTTP requests as they are logged:sudo tail -f /var/log/nginx/http_requests.log
Example Log Entry:
192.168.1.100 - - [01/Jan/2025:10:00:00 +0000] "GET /ords/r/myapp/ HTTP/1.1" 200 1234 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"
This log entry includes the client's IP address, request details, response status, and user agent.
Step 5: Additional Tips
Rotate Logs: To prevent the log file from growing too large, set up log rotation using a tool like
logrotate
.Secure Log Files: Restrict access to log files to protect sensitive information.
sudo chmod 640 /var/log/nginx/http_requests.log sudo chown root:adm /var/log/nginx/http_requests.log
Conclusion
With HTTP logging configured in NGINX, you can track every request made to your server. This setup is essential for debugging, security analysis, and performance monitoring. Remember to regularly review your logs and ensure they are securely stored.
Contact for your custom installation Richmond - richmond@cloudifyhub.com