Yes, you can run Docker Compose with Nginx and WordPress protected by a ModSecurity Web Application Firewall (WAF) using the OWASP Core Rule Set (CRS). This article explains exactly how to set it up, what each component does, and how to avoid common pitfalls so you get a working, hardened stack.
Why Combine Nginx, WordPress, and ModSecurity?
WordPress powers a large share of the web, making it a frequent target for attacks like SQL injection, cross-site scripting (XSS), and brute-force login attempts. A WAF sits in front of your application and filters incoming traffic before it reaches WordPress. ModSecurity is a widely used open-source WAF engine, and the OWASP CRS provides a set of rules that detect and block common attack patterns. Nginx serves as a fast, efficient web server and reverse proxy, and Docker Compose lets you define and run the whole stack—Nginx, WordPress, and the WAF—as reproducible containers.
Architecture Overview
In a typical Docker Compose setup, you have three main services:
- Nginx container: acts as the entry point, serving static files and proxying PHP requests to the WordPress container.
- WordPress container: runs PHP-FPM and contains the WordPress application files.
- Database container (usually MySQL or MariaDB): stores WordPress data.
ModSecurity can be integrated in two ways: as a module inside the Nginx container (using the libmodsecurity module) or as a separate reverse proxy container (for example, using the owasp/modsecurity-crs image). The separate proxy approach is cleaner because it isolates the WAF and allows you to update rules independently. In that setup, traffic flows: client → ModSecurity proxy → Nginx → WordPress → database.
Step-by-Step Docker Compose Configuration
Here is a practical example using the ModSecurity proxy approach. Create a docker-compose.yml file with the following services:
- ModSecurity proxy: use the official
owasp/modsecurity-crsimage, which includes ModSecurity and the OWASP CRS. Configure it to listen on port 80 and forward traffic to the Nginx service. - Nginx: use the official
nginximage. Create a custom configuration that listens on port 8080 (internal) and proxies PHP requests to the WordPress service. - WordPress: use the official
wordpress:php8.2-fpmimage, which includes PHP-FPM and the WordPress code. - Database: use
mysql:8.0ormariadb:10.11with a volume for persistent storage.
Example snippet for the ModSecurity service:
modsecurity:
image: owasp/modsecurity-crs:latest
ports:
- "80:80"
environment:
- PARANOIA=1
- MODSEC_RULE_ENGINE=On
- BACKEND=http://nginx:8080
volumes:
- ./modsecurity.conf:/etc/modsecurity/modsecurity.conf:ro
Note: the BACKEND environment variable tells the proxy where to forward requests. In this example, it points to the internal Nginx service.
Configuring Nginx for WordPress
Nginx needs to handle static files and pass PHP requests to the WordPress container. Create an nginx.conf that includes the standard WordPress rewrite rules. A minimal configuration looks like:
server {
listen 8080;
server_name _;
root /var/www/html;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass wordpress:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Mount this file into the Nginx container as /etc/nginx/conf.d/default.conf. Ensure the WordPress container is named wordpress in the Compose network so Nginx can resolve it.
ModSecurity and OWASP CRS Configuration
The OWASP CRS image comes with sensible defaults, but you may want to tune it. Key environment variables include:
PARANOIA: sets the rule paranoia level (1-4). Level 1 is recommended for production; higher levels cause more false positives.MODSEC_RULE_ENGINE: set toOnto enforce rules, orDetectionOnlyto log without blocking.MODSEC_AUDIT_ENGINE: controls audit logging; set toRelevantOnlyto reduce log noise.MODSEC_TX_ANOMALY_SCORE: the threshold for blocking; default is 5, which is reasonable.
You can also mount a custom modsecurity.conf to override defaults, but the environment variables are usually sufficient.
Common Pitfalls and Troubleshooting
Several issues frequently trip up users:
- Port conflicts: if you already have a web server on port 80, change the host port mapping (e.g.,
"8080:80"). - Nginx cannot connect to WordPress: ensure both services are on the same Docker network and use the correct service name as hostname.
- WordPress shows “Error establishing a database connection”: check the database hostname in
wp-config.php; it should be the database service name (e.g.,db). - False positives from ModSecurity: start with
PARANOIA=1andMODSEC_RULE_ENGINE=DetectionOnlyto monitor logs, then adjust rules or whitelist specific paths. - Static files not loading: ensure the Nginx root points to the same WordPress volume; use a shared volume for
/var/www/html.
Performance and Security Considerations
Running a WAF adds overhead because every request is inspected. For low-traffic sites this is negligible, but for high-traffic sites consider:
- Using a dedicated ModSecurity container with more CPU/memory limits.
- Enabling caching for static assets (e.g., Nginx
expiresdirective). - Placing the WAF behind a CDN or load balancer that handles static content.
Security-wise, always keep the OWASP CRS updated by pulling the latest image. Also, secure the database container with a strong password and avoid exposing its port to the host.
Alternatives and When to Use Them
If you prefer a different approach, consider:
- Nginx ModSecurity module: compile Nginx with ModSecurity directly; this reduces one container but makes updates more complex.
- Cloud WAF services: managed services like AWS WAF or Cloudflare can offload WAF processing, but they are not self-hosted.
- Other WAF engines: like NAXSI (Nginx Anti XSS & SQL Injection) which is simpler but less comprehensive than ModSecurity.
For most self-hosted WordPress setups, the Docker Compose approach with a separate ModSecurity proxy offers the best balance of control, ease of update, and isolation.
Frequently Asked Questions
Do I need a separate ModSecurity container?
Not necessarily. You can compile ModSecurity into Nginx, but the separate container is easier to maintain and update independently. It also allows you to scale the WAF separately if needed.
How do I handle false positives?
Start in DetectionOnly mode, review the audit logs, and then either adjust the paranoia level or add exclusions for specific rules that affect legitimate functionality (e.g., file uploads).
Can I use this with an existing WordPress site?
Yes. Mount your existing WordPress files as a volume and configure the database connection accordingly. The WAF will filter traffic regardless of whether WordPress is new or existing.
What is the performance impact of ModSecurity?
It adds a small latency (typically 1-5 ms per request) and consumes CPU. For most sites, this is acceptable, but you should load-test if you expect high traffic.
Is this setup production-ready?
Yes, with proper tuning. Use paranoia level 1, set the anomaly score to a reasonable threshold, and monitor logs regularly. Also, ensure you have backups and follow WordPress security best practices.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Microsoft Surface Duo: The Foldable Phone Story
- Samsung SM-T813 Hard Reset: Step-by-Step Guide
- Mafia Clash Codes: Real Meanings & Usage
- Remove Programs from Mac: Full Uninstall Guide
- Project Sentinel Morning Digest: Data Stack Failure Alerts
- Best KVM VPS Hosting: Top Picks & Guide