nginx-request-logging
Original:🇺🇸 English
Translated
Guide for configuring Nginx web server with custom request logging, rate limiting, and error pages. This skill should be used when tasks involve Nginx installation, configuration, custom log formats, rate limiting setup, or custom error page creation.
2installs
Sourceletta-ai/skills
Added on
NPX Install
npx skill4agent add letta-ai/skills nginx-request-loggingTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →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
/etc/nginx/nginx.conf - Check for existing directives to understand file organization
include - Identify where log formats, rate limiting zones, and other global settings are defined
- Read
-
Check system state
- Verify if Nginx is already installed: or
which nginxnginx -v - Check if Nginx is already running: or
pgrep nginxps aux | grep nginx - Verify if the target port is available: or
ss -tlnp | grep <port>netstat -tlnp | grep <port>
- Verify if Nginx is already installed:
-
Backup original configuration
- Create a backup before modifications:
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
- Create a backup before modifications:
Configuration Approach
Directory Structure
Nginx configurations typically follow this hierarchy:
- - Main configuration (global settings, log formats, rate limiting zones)
/etc/nginx/nginx.conf - - Site-specific configurations (server blocks)
/etc/nginx/conf.d/ - and
/etc/nginx/sites-available/- Alternative site management (Debian-based)/etc/nginx/sites-enabled/
Configuration Placement Guidelines
| Setting Type | Location | Reason |
|---|---|---|
| Log format definitions | | Must be defined before use in server blocks |
| Rate limiting zones | | 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:
bash
nginx -tVerification Strategies
Basic Functionality
bash
curl -s http://localhost:<port>/
curl -s -o /dev/null -w "%{http_code}" http://localhost:<port>/nonexistentRate 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, sequentialLog Verification
bash
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.logCommon 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 withor similar
xargs -P -
Configuration syntax errors: Always runbefore starting or reloading
nginx -t -
Port already in use: Check withbefore configuring a new port
ss -tlnp -
systemctl not available: In containers or minimal environments, usecommand directly
nginx -
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 ,
mkdir -pto handle existing/missing files gracefullyrm -f
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
nginx -t - Start/reload Nginx service
- Verify all functionality (main page, error pages, rate limiting, logs)