Agent skill
nginx-request-logging
Guidance for configuring Nginx web servers with custom logging, rate limiting, and static content serving. This skill should be used when tasks involve setting up Nginx server blocks, configuring custom log formats, implementing rate limiting, or serving static files on specific ports.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/development/nginx-request-logging
SKILL.md
Nginx Request Logging
Overview
This skill provides guidance for configuring Nginx web servers with custom access logging, rate limiting zones, and static content serving. It covers common configuration patterns, verification strategies, and pitfalls to avoid when setting up production-ready Nginx configurations.
Pre-Configuration Checks
Before making any configuration changes, perform these essential checks:
- Check for existing Nginx processes: Run
pgrep nginxorps aux | grep nginxto determine if Nginx is already running - Verify port availability: Use
ss -tlnp | grep <port>ornetstat -tlnp | grep <port>to check if the target port is in use - Confirm directory structure: Verify that
/etc/nginx/conf.d/exists before writing configuration files - Check log directory permissions: Ensure
/var/log/nginx/is writable for custom log files - Back up existing configuration: Copy current nginx.conf before modifications
Configuration Workflow
Step 1: Define Custom Log Format
Add custom log formats in the http block of /etc/nginx/nginx.conf:
log_format custom_format '$time_local $request_method $status "$http_user_agent"';
Key considerations:
- Log format must be defined in the
httpblock, not within aserverblock - Variable names are case-sensitive (e.g.,
$http_user_agentnot$HTTP_USER_AGENT) - Quoted fields in the format require escaped quotes or single quotes around the entire format
Step 2: Configure Rate Limiting
Rate limiting requires two components:
- Zone definition (in
httpblock):
limit_req_zone $binary_remote_addr zone=myzone:10m rate=10r/s;
- Zone application (in
serverorlocationblock):
limit_req zone=myzone burst=10 nodelay;
Rate limiting behavior options:
- Without
nodelay: Burst requests are queued and processed at the defined rate - With
nodelay: Burst requests are processed immediately; excess requests rejected - Without
burst: Only the base rate is allowed; no bursting permitted
Step 3: Server Block Configuration
Create a server block in /etc/nginx/conf.d/ with:
server {
listen <port>;
server_name localhost;
root /var/www/<site>;
index index.html;
access_log /var/log/nginx/<logfile>.log custom_format;
location / {
limit_req zone=myzone burst=10 nodelay;
try_files $uri $uri/ =404;
}
}
Step 4: Validate and Apply Configuration
- Test configuration syntax:
nginx -t - Reload or start Nginx:
- If running with systemd:
systemctl reload nginx - If systemd unavailable:
nginx -s reload(for reload) ornginx(for start)
- If running with systemd:
- Verify the process is running:
pgrep nginxorps aux | grep nginx
Verification Strategies
Basic Connectivity Test
curl -s http://localhost:<port>/
Log Format Verification
After making requests, inspect the log file:
tail -5 /var/log/nginx/<logfile>.log
Verify each expected field is present and correctly formatted.
Rate Limiting Verification
Test rate limiting with concurrent requests:
for i in $(seq 1 25); do curl -s -o /dev/null -w "%{http_code}\n" http://localhost:<port>/; done
Expected behavior:
- Initial requests (up to rate + burst) return 200
- Excess requests return 503 (Service Temporarily Unavailable)
With burst=10 and rate=10r/s:
- First ~11-20 requests typically succeed (depends on timing)
- Remaining requests return 503
Common Pitfalls
Configuration Errors
- Log format in wrong block: Custom log formats must be in the
httpblock, notserver - Missing semicolons: Every Nginx directive must end with a semicolon
- Incorrect variable names: Use
$binary_remote_addrfor rate limiting (more efficient than$remote_addr) - Forgetting to test config: Always run
nginx -tbefore reload/restart
Process Management Errors
- Starting when already running: Check for existing processes before starting
- Using wrong reload method: If systemctl fails, use
nginx -s reload - Permission issues: Log directories and HTML root must be accessible by the nginx worker user (typically
www-dataornginx)
Testing Errors
- Echo compatibility: Use
printfinstead ofecho -efor portable newline handling - Interpreting rate limit results: Results vary based on request timing; don't expect exact counts
- Not checking actual log output: Always verify logs contain expected format, not just that requests succeed
Rollback Strategy
If configuration changes cause issues:
- Restore backup:
cp /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf - Remove problematic conf.d files:
rm /etc/nginx/conf.d/<problematic>.conf - Test and reload:
nginx -t && nginx -s reload
References
For detailed configuration syntax and directives, refer to references/nginx_configuration.md.
Didn't find tool you were looking for?