One Nginx, Three Vhosts: Separating Assets, CMS, and Frontend
Three nginx server blocks separate media, the WordPress admin, and the Astro frontend with different security profiles, plus a healthcheck-gated startup order.
TL;DR
The stack runs nginx over WordPress, MariaDB, and an Astro frontend, but one nginx file juggling media, PHP, and proxying invites config leaks. Splitting into three server blocks isolates security profiles, with regex denies stopping PHP execution in uploads. Nginx add_header inheritance gotchas force repeated snippet includes, while compose healthchecks ensure MariaDB readiness.
The compose file came up clean: mariadb, wordpress, astro, nginx. Then the nginx config stared back at me as one file that had to do three unrelated jobs. Serve static media from wp-content/uploads. Run the WordPress admin and PHP backend. Proxy the Astro frontend listening on port 4321. One container, three security profiles, one config directory.
The first instinct was to keep it in a single server block. Route by path: location /wp-admin, location /uploads, location /. It reads simpler on paper.
It is not simpler. The moment you mix a public static cache policy with a private PHP backend in the same vhost, every location block becomes a place where a header, an expiry, or an access rule can leak into the wrong surface. Path routing also assumes the browser reaches all three jobs through one hostname, which is exactly what a migration away from classic WordPress should not assume.
So the split: three server blocks, three hostnames, three profiles.
Three server blocks, three security profiles
The assets vhost serves media and nothing else. Its root points at the uploads directory on the shared backend volume, nothing under it is ever executed, and everything is cacheable forever.
# assets vhost: static media, no PHP
server {
listen 80;
server_name assets.example.com;
root /var/www/html/wp-content/uploads;
include /etc/nginx/snippets/security-headers.conf;
location / {
expires max;
add_header Cache-Control "public, immutable";
try_files $uri =404;
}
location ~* \.(php|phtml|phar)$ {
deny all;
}
}
The regex location is the important one. Uploads directories are the classic way a compromised media file becomes remote code execution; denying every .php, .phtml, and .phar request at the web server means a malicious upload is a dead file, not a shell.
The CMS vhost listens on 8085 and never gets published on a public port. Only the docker network and a local port-forward reach it. It includes a small php-security snippet: deny dotfiles such as .env, deny xmlrpc.php, and hide the X-Powered-By header.
Its PHP location hands off to wordpress:9000 over FastCGI. The WordPress documentation calls xmlrpc.php a frequent brute-force target, abused through the system.multicall method to try many passwords in one request, and advises disabling it when nothing needs it [5]. The config also carries a commented-out basic-auth placeholder. It is a note for later, not an active control.
The main vhost is default_server and does almost nothing: proxy_pass http://astro:4321, plus a location block on /nginx-health returning the plain text ok for monitoring.
The header trap inside location
The first version of the assets vhost lost its security headers. The snippet was included at server level, the media location set add_header Cache-Control, and suddenly a curl -I against a JPEG showed only Cache-Control. The rest vanished.
That is the documented behavior, not a bug: nginx inherits add_header directives from the previous configuration level if and only if there are no add_header directives on the current level [3]. One add_header inside location / silently cancels every header inherited above it.
Two fixes exist. Drop the add_header Cache-Control line and let expires max set caching alone, since expires does not follow that inheritance rule. Or repeat the include inside the location, which keeps the immutable directive and the security headers together.
# fix: repeat the snippet inside the location
location / {
include /etc/nginx/snippets/security-headers.conf;
expires max;
add_header Cache-Control "public, immutable";
try_files $uri =404;
}
This stack pins nginx at 1.27-alpine, which predates the add_header_inherit merge directive added in 1.29.3 [3], so the repeat-include pattern is the only option on this version. Verify it yourself: curl -I against any media file on assets.example.com should return the three security headers next to Cache-Control: public, immutable. Only Cache-Control means a stray add_header is still shadowing the inheritance somewhere.
Compose: health before start
The compose layout is a base file plus two overlays: dev adds bind mounts for live editing, prod switches to named volumes, sets a restart policy, and publishes only nginx on 80 and 443. Two bridge networks keep the boundaries explicit: an internal network for mariadb and wordpress, a web network for nginx, wordpress, and astro. The CMS port is never mapped to the host in prod.
MariaDB readiness uses the exact healthcheck pattern from the official documentation [4], and wordpress waits on it because Compose by default does not wait until a container is ready, only until it is running [1]:
services:
mariadb:
image: mariadb:11.4
healthcheck:
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
wordpress:
depends_on:
mariadb:
condition: service_healthy
Secrets stay out of the files. Required values use ${VAR:?error} interpolation, for example MARIADB_ROOT_PASSWORD: ${MARIADB_ROOT_PASSWORD:?set in .env}, so a missing variable aborts docker compose up immediately instead of booting a half-configured database [2].
The proof is one command: docker compose ps shows mariadb reporting healthy before the wordpress container ever starts.