NGINX + ModSecurity WAF
NGINX with ModSecurity Web Application Firewall for security.
Overview
NGINX is a high-performance HTTP server and reverse proxy that has become the backbone of modern web infrastructure since its creation in 2004. Originally developed to solve the C10K problem of handling 10,000 concurrent connections, NGINX uses an event-driven, asynchronous architecture that dramatically outperforms traditional process-based web servers under high load conditions. Its lightweight footprint and exceptional performance characteristics make it ideal for serving static content, load balancing, and proxying requests to backend applications.
This configuration combines NGINX with ModSecurity, the gold standard open-source Web Application Firewall (WAF) that inspects HTTP traffic in real-time to detect and block malicious requests. ModSecurity uses the OWASP Core Rule Set (CRS), a comprehensive collection of rules designed to protect against common web vulnerabilities including SQL injection, cross-site scripting (XSS), remote file inclusion, and other OWASP Top 10 threats. The integration creates a powerful security layer that filters malicious traffic before it reaches your application servers.
This stack is essential for organizations running public-facing web applications that need enterprise-grade security without the cost of commercial WAF solutions. Security-conscious developers, system administrators protecting legacy applications, and compliance teams meeting regulatory requirements will find this combination provides robust protection with granular control over security policies. The OWASP ModSecurity CRS comes pre-configured with battle-tested rules while allowing extensive customization for specific application needs.
Key Features
- OWASP Core Rule Set (CRS) protection against SQL injection, XSS, and remote file inclusion attacks
- Configurable paranoia levels from 1-4 to balance security strictness with false positive rates
- Real-time HTTP traffic inspection with anomaly scoring to detect complex attack patterns
- Comprehensive request and response body analysis including file upload scanning
- IP reputation blocking and rate limiting to prevent brute force and DDoS attacks
- Detailed security logging with structured JSON output for SIEM integration
- Rule customization support through SecRule directives and whitelist exceptions
- Event-driven NGINX architecture handling thousands of concurrent connections efficiently
Common Use Cases
- 1Protecting e-commerce platforms from payment card data theft and fraud attempts
- 2Securing WordPress and CMS installations against automated vulnerability scanners
- 3Compliance requirements for PCI DSS, HIPAA, or SOX mandating WAF deployment
- 4Legacy application security hardening without modifying source code
- 5API endpoint protection against injection attacks and malformed requests
- 6Multi-tenant SaaS platforms requiring tenant-specific security policies
- 7Educational institutions protecting student information systems from attacks
Prerequisites
- Minimum 512MB RAM recommended for ModSecurity rule processing and logging
- Available ports 80 and 443 for HTTP/HTTPS traffic handling
- Understanding of HTTP request/response flow and common web vulnerabilities
- Familiarity with NGINX configuration syntax and ModSecurity rule format
- Log monitoring strategy for analyzing blocked requests and tuning rules
- SSL/TLS certificates if enabling HTTPS protection
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 nginx-waf: 3 image: owasp/modsecurity-crs:nginx4 container_name: nginx-waf5 restart: unless-stopped6 environment: 7 PARANOIA: 18 ANOMALY_INBOUND: 59 ANOMALY_OUTBOUND: 410 volumes: 11 - ./nginx/conf.d:/etc/nginx/conf.d:ro12 ports: 13 - "80:80"14 - "443:443"15 networks: 16 - waf-network1718networks: 19 waf-network: 20 driver: bridge.env Template
.env
1# ModSecurity paranoia level (1-4)2PARANOIA=13# Anomaly threshold4ANOMALY_INBOUND=55ANOMALY_OUTBOUND=4Usage Notes
- 1Docs: https://coreruleset.org/docs/ and https://modsecurity.org/
- 2OWASP Core Rule Set (CRS) pre-configured for common attacks
- 3PARANOIA=1 (low) to 4 (high) - higher = more strict, more false positives
- 4View blocked requests: docker logs nginx-waf | grep 'ModSecurity'
- 5Whitelist false positives with SecRuleRemoveById in custom rules
- 6Test rules before production: set SecRuleEngine DetectionOnly
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 nginx-waf:5 image: owasp/modsecurity-crs:nginx6 container_name: nginx-waf7 restart: unless-stopped8 environment:9 PARANOIA: 110 ANOMALY_INBOUND: 511 ANOMALY_OUTBOUND: 412 volumes:13 - ./nginx/conf.d:/etc/nginx/conf.d:ro14 ports:15 - "80:80"16 - "443:443"17 networks:18 - waf-network1920networks:21 waf-network:22 driver: bridge23EOF2425# 2. Create the .env file26cat > .env << 'EOF'27# ModSecurity paranoia level (1-4)28PARANOIA=129# Anomaly threshold30ANOMALY_INBOUND=531ANOMALY_OUTBOUND=432EOF3334# 3. Start the services35docker compose up -d3637# 4. View logs38docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/nginx-modsecurity/run | bashTroubleshooting
- High false positive rates blocking legitimate traffic: Lower PARANOIA level from 2 to 1 or add SecRuleRemoveById exceptions for specific rules
- ModSecurity blocking file uploads or form submissions: Increase SecRequestBodyLimit and SecRequestBodyNoFilesLimit in configuration
- Performance degradation with ModSecurity enabled: Disable response body inspection with SecResponseBodyAccess Off and tune paranoia level
- Rules not loading or CRS errors in logs: Verify OWASP_CRS environment variables match available rule sets in the container image
- Cannot access blocked request details: Enable audit logging with SecAuditEngine On and check /var/log/modsec_audit.log
- NGINX failing to start with ModSecurity module: Ensure modsecurity.conf includes proper SecRuleEngine directive and valid CRS path
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Ad Space
Shortcuts: C CopyF FavoriteD Download