Nginx Request Logging Configuration
This skill provides guidance for configuring Nginx web servers with custom logging, rate limiting, and error handling.
When to Use This Skill
Apply this skill when tasks involve:
- Installing and configuring Nginx
- Setting up custom log formats
- Implementing rate limiting
- Creating custom error pages (404, 500, etc.)
- Configuring Nginx to listen on non-standard ports
Pre-Configuration Analysis
Before modifying any Nginx configuration:
-
Examine existing configuration structure
- Read to understand the current setup
- Check for existing directives to understand file organization
- Identify where log formats, rate limiting zones, and other global settings are defined
-
Check system state
- Verify if Nginx is already installed: or
- Check if Nginx is already running: or
- Verify if the target port is available: or
netstat -tlnp | grep <port>
-
Backup original configuration
- Create a backup before modifications:
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
Configuration Approach
Directory Structure
Nginx configurations typically follow this hierarchy:
- - Main configuration (global settings, log formats, rate limiting zones)
- - Site-specific configurations (server blocks)
/etc/nginx/sites-available/
and /etc/nginx/sites-enabled/
- Alternative site management (Debian-based)
Configuration Placement Guidelines
| Setting Type | Location | Reason |
|---|
| Log format definitions | (http block) | Must be defined before use in server blocks |
| Rate limiting zones | (http block) | Zones are shared across server blocks |
| Server blocks | | Modular, easy to manage |
| Custom error pages | Server block or location block | Context-specific |
Rate Limiting Configuration
Rate limiting requires two parts:
-
Zone definition (in http block of nginx.conf):
nginx
limit_req_zone $binary_remote_addr zone=zonename:10m rate=10r/s;
-
Zone application (in server or location block):
nginx
limit_req zone=zonename burst=5 nodelay;
Custom Log Format
Define custom log formats in the http block:
nginx
log_format custom_format '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
Apply in server block:
nginx
access_log /var/log/nginx/custom_access.log custom_format;
Service Management
Nginx service management varies by environment:
| Environment | Start Command | Reload Command | Stop Command |
|---|
| systemd | | | |
| Direct | | | |
| Docker/Container | | | |
Important: Always test configuration before starting/reloading:
Verification Strategies
Basic Functionality
bash
curl -s http://localhost:<port>/
curl -s -o /dev/null -w "%{http_code}" http://localhost:<port>/nonexistent
Rate Limiting Verification
Rate limiting requires concurrent requests to trigger. Sequential requests will not exceed the rate limit.
Correct approach (parallel requests):
bash
seq 20 | xargs -P 20 -I {} curl -s -o /dev/null -w "%{http_code}\n" http://localhost:<port>/
Incorrect approach (will not trigger rate limiting):
bash
for i in {1..20}; do curl -s http://localhost:<port>/; done # Too slow, sequential
Log Verification
bash
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
Common Pitfalls
-
Log format not found: Log format must be defined in nginx.conf before being referenced in server blocks
-
Rate limiting not triggering: Sequential requests are too slow; use parallel requests with
or similar
-
Configuration syntax errors: Always run
before starting or reloading
-
Port already in use: Check with
before configuring a new port
-
systemctl not available: In containers or minimal environments, use
command directly
-
Default site conflicts: Remove or disable default site configuration when creating custom configurations:
bash
rm -f /etc/nginx/sites-enabled/default
-
Missing directories: Verify required directories exist before writing configuration:
bash
ls -la /etc/nginx/conf.d/
Execution Efficiency
- Batch file operations: Create multiple static files (index.html, 404.html, etc.) in parallel when possible
- Combine verification steps: Test multiple endpoints in a single verification pass
- Plan verification upfront: Determine the testing strategy before implementation
- Use idempotent commands: Prefer , to handle existing/missing files gracefully
Example Workflow
- Check system state (Nginx installed, running, port availability)
- Read existing nginx.conf structure
- Backup configuration
- Create required directories and static content
- Modify nginx.conf for global settings (log format, rate limiting zone)
- Create server configuration in conf.d/
- Remove conflicting default configurations
- Test configuration with
- Start/reload Nginx service
- Verify all functionality (main page, error pages, rate limiting, logs)