nginx in front of Apache (TLS proxy on :8008)
nginxapachereverse-proxycertbotipv6vhost
On the Alma 9 server every site uses the same two-tier layout: nginx listens on 80/443,
terminates TLS, redirects HTTP to HTTPS, and reverse-proxies to Apache (httpd) on
127.0.0.1:8008, which serves the files and runs PHP via php-fpm. Adopted 2026-03-15. Knowing
which tier owns which job explains most "why is this broken" questions.
Who does what
| Concern | Owner |
|---|---|
| TLS certificates, HTTP to HTTPS 301 | nginx (certbot --nginx) |
Upload size limit (client_max_body_size) |
nginx (default 1M!) |
Upstream timeout (proxy_read_timeout) |
nginx (default 60s) |
| gzip, long-lived cache headers for static paths | nginx (per site) |
| DocumentRoot, rewrites, SPA fallback, aliases | Apache vhost on *:8008 |
| CORS headers | Apache (works fine behind the proxy) |
| PHP | php-fpm, called from Apache |
Apache vhosts have no SSL directives at all. Old HTTP-only redirect vhosts were disabled because nginx owns redirects.
Files
- nginx:
/etc/nginx/conf.d/<site>.conf(some older sites shareproxy.conf). - Apache:
/etc/httpd/conf.d/<site>.conf,<VirtualHost *:8008>withServerName. - Keep a copy of both in the project repo (
deploy/nginx-<site>.conf,deploy/httpd-<site>.conf); certbot edits the server copy to add the SSL lines, so the repo copy is the pre-certbot version.
Adding a new site
- Point DNS (A, and AAAA if you have one) at the server.
- Write the Apache vhost on :8008 and run
apachectl configtest. A warning that the DocumentRoot does not exist is harmless if the first deploy has not run yet. - Write the nginx server block that
proxy_passes tohttp://127.0.0.1:8008and setsHost,X-Forwarded-ForandX-Forwarded-Proto. sudo certbot --nginx -d example.com -d www.example.com(auto-renew is already set up). For a<name>.jhampton.iosite skip this step: point the 443 block at the wildcard certificate under/etc/letsencrypt/live/wildcard.jhampton.io/instead (jhampton.io: one home domain, an index, and a wildcard certificate); copying an existing site'sserver/nginx-<name>.jhampton.io.conf(e.g. umlol's) gives a template that already includes the :80 → https redirect.systemctl reload httpd nginx, then curl bothhttp://(expect 301) andhttps://.
Apps that need to know the request was HTTPS (for Secure cookies, absolute URLs) must read
X-Forwarded-Proto, because PHP only sees plain HTTP from nginx.
Exception: nginx-only sites (no Apache hop)
A static single-page app (Flutter web build) with a small PHP API does not need Apache.
music.hampton.website (July 2026, conf /etc/nginx/conf.d/music.conf, modeled on
forge.conf) is served entirely by nginx:
- Static files straight from the docroot (/var/www/html/music/app/), with no-cache on
entry points (index.html, flutter_bootstrap.js, service worker, manifest) and
1-year immutable caching on hashed assets, so deploys show up at once.
- The PHP endpoint goes fastcgi_pass directly to the php-fpm upstream. php-fpm's pool
runs as user apache, so any config file the script includes (for example a server-only
s3_conf.php) must be readable by group apache, or PHP fails to load it.
- Deploy: rsync the build to a staging dir in the home directory, then a sudo script
rsyncs it into the docroot and runs restorecon (see SELinux file contexts and booleans on web servers).
How the layout was introduced (2026-03-15)
nginx was put in front originally so a webhook path could be proxied to a reverse SSH tunnel
(claude-service on the desktop, port 9100), which Apache could not easily do under SELinux.
The migration steps, useful if it ever has to be redone on a new box:
- httpd.conf Listen 80 became Listen 127.0.0.1:8008; Listen 443 in ssl.conf was commented
out; every <VirtualHost *:80>/*:443 became *:8008; all SSL directives were removed from
Apache vhosts; the default server block in nginx.conf was disabled.
- Port 8008 was chosen over 8080 because SELinux already labels it http_port_t;
httpd_can_network_connect was already on and covers nginx too (same httpd_t domain).
- Existing certbot renewal configs were switched to authenticator = nginx and
installer = nginx. The nginx plugin edits the site's .conf in place (adds
listen 443 ssl, cert paths and the redirect block); the Apache plugin used to create separate
-le-ssl.conf files. No new file appearing is normal.
- The nginx server block must exist before running certbot --nginx -d ... for a new site.
Gotchas
- Listen on IPv6 too. Every server block needs both
listen 80;andlisten [::]:80;(and the same for 443) for any domain with an AAAA record. Let's Encrypt prefers IPv6 when an AAAA exists; with onlylisten 80;the http-01 challenge lands on the wrong server block and 404s, and the cert silently fails to renew (hampton.website expired this way, fixed 2026-07-08). Symptom in/var/log/letsencrypt/letsencrypt.log: "Invalid response ... 404" from an IPv6 address. - Missing Apache vhost falls through to the default one. If the
ServerNamevhost is missing, Apache answers with the first:8008vhost. On this server that was therunAPI, so justinahampton.com returned{"status":"ok","app":"run"}. Unexpected JSON from another app means a missing or misnamed vhost. - Upload limits. nginx's per-site default
client_max_body_sizeis 1M; a bigger upload is rejected by nginx before PHP ever sees it (413). Raise it per site (the tldrcom block is 512M, plumb 12m) and keep PHP'supload_max_filesize/post_max_sizeconsistent. - Slow upstreams. Requests that call an LLM or other slow API can outlast nginx's 60 s
default; plumb sets
proxy_read_timeout 300s. - A domain without AAAA. If DNS has only an A record, IPv4 works but IPv6-only clients
cannot resolve the site. Check with
dig AAAA. - New subdomains and local DNS caches. A home router can cache NXDOMAIN for ~45 minutes after
a record is created. Check
dig @1.1.1.1 hostand test withcurl --resolve host:443:45.79.9.71 https://host/before deciding the site is down. - Unknown hostnames hit the catch-all.
/etc/nginx/conf.d/00-default.confis thedefault_server: a name no block claims gets 444 (connection closed) on :80 and a refused TLS handshake on :443. Before it existed (until 2026-10-30) nginx served such names from the first block loaded, which was 3d.justinahampton.com with its own certificate. A handshake error for a new site therefore usually means its server block is missing or misnamed, not that the site is down. - Install a replacement config under the filename it replaces. A redirect block installed as
a new file left the old file loaded too; nginx logged "conflicting server name ... ignored" and
kept serving the old block.
nginx -tstill exits 0 in that case, so read its warnings. - Non-HTTP backends behind nginx (e.g. a webhook proxied to a tunnel on port 9100) need the
port labeled
http_port_tin SELinux:semanage port -a -t http_port_t -p tcp 9100. Port 8008 already has that label.
See also
- Setting up an Alma Linux 9 LAMP server
- Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects)
- SELinux file contexts and booleans on web servers
- claude-service (ProcrastinAI's webhook runner for headless Claude)
Linked from
- A self-hosted F-Droid repository and APK catalog
- Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects)
- claude-service (ProcrastinAI's webhook runner for headless Claude)
- Debugging client-side bugs from the server's access log
- Deploy scripts with rsync (patterns and traps)
- Hosting one static site per subdomain with a wildcard vhost
- Inline blur-up placeholders (LQIP): JPEG beats WebP across a page
- justinahampton.com and its subdomains
- Live ghost-racing multiplayer (design research)
- Moving a website to a new server without downtime
- Release-directory deploys with an atomic symlink swap
- SELinux file contexts and booleans on web servers
- Sending mail from PHP with Postfix, SPF, DKIM and DMARC
- Setting up an Alma Linux 9 LAMP server
- Slim 4 PHP APIs (and migrating from Limonade)
- TLDR platform architecture (tldrnerd.com)
- Upload links for people without an account (getting a full-quality video from a friend)