Hosting one static site per subdomain with a wildcard vhost
apachevirtualdocumentrootwildcard-dnsnginxcertbotlets-encryptstatic-hostingsecurity-headers
To give many users (or projects) each their own site at <slug>.example.com without writing a vhost
per site, combine a wildcard DNS record, one Apache vhost using VirtualDocumentRoot, and a
directory per slug. Each slug is then its own browser origin, which matters when the content is
untrusted: one site's scripts cannot read another's cookies or storage. Used for the AI guest sites
at *.ai.tldrcom.com (first one: aaron.ai.tldrcom.com), whose content is written by a sandboxed AI
agent and must never execute on the server.
DNS
One wildcard A record, *.ai.tldrcom.com -> 45.79.9.71, covers every future slug; adding a site
needs no DNS change. A wildcard matches only one label (a.ai.tldrcom.com, not
b.a.ai.tldrcom.com).
Apache (behind nginx on :8008)
<VirtualHost *:8008>
ServerName ai.tldrcom.com
ServerAlias *.ai.tldrcom.com
UseCanonicalName Off
VirtualDocumentRoot /var/www/html/ai-guest/%1
<Directory /var/www/html/ai-guest>
Options +Indexes -ExecCGI -Includes -FollowSymLinks +SymLinksIfOwnerMatch
AllowOverride None
Require all granted
AddType text/plain .php .phtml .phar .cgi .pl .py .sh
</Directory>
<Location "/">
SetHandler none
</Location>
Header always set X-Content-Type-Options "nosniff"
Header always unset Set-Cookie
</VirtualHost>
%1is the first label of the requested host, soaaron.ai.tldrcom.comserves/var/www/html/ai-guest/aaron/. A host with no directory gets a 404.UseCanonicalName Offmakes self-referential URLs use the requested host, notServerName.- Static only:
<Location "/"> SetHandler nonewins over every<Files>/<FilesMatch>block (including the global php.conf handler), because Location sections merge last (Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects)). No file can reach php-fpm or CGI;.phpis served as text. Add a CSP,nosniff, no-referrer andframe-ancestors 'none'to taste. AllowOverride Noneso uploaded.htaccessfiles cannot re-enable anything.
nginx and TLS
- One port-80 server block with
server_name *.ai.tldrcom.com;proxies everything to Apache. - No wildcard certificate: Let's Encrypt issues wildcards only through the DNS-01 challenge,
which needs API access to the DNS provider (none here; the zone is in Linode's DNS Manager).
Instead each slug gets its own small server block from a template plus
certbot --nginx -d <slug>.ai.tldrcom.com --redirect -n. A helper script creates the directory and the block, runsrestorecon, tests and reloads nginx. - A brand-new name may not resolve everywhere at once. The guest setup used a detached waiter that
polls
dig @1.1.1.1until the record answers, waits two more minutes for the other resolvers Let's Encrypt uses, then runs certbot and verifies HTTPS. See the NXDOMAIN-caching note in nginx in front of Apache (TLS proxy on :8008).
Publishing content
The content is produced elsewhere and pushed with rsync:
rsync -rt --no-links --chmod=D755,F644 --delete public/ server:/var/www/html/ai-guest/<slug>/
then restorecon -R. --no-links keeps symlinks out; forced modes keep everything world-readable
and nothing executable.
Gotchas
--deletepublishing erases hand-added files. Anything placed on the server by hand vanishes on the next publish; put it in the sourcepublic/directory instead.- Never host untrusted content on a path of a site that has logins (e.g.
tldrcom.com/guest/): it would share that site's origin, cookies and storage. Subdomains are separate origins for scripts and storage, but a cookie the parent sets withDomain=example.comis still sent to every subdomain, so keep the parent's cookies host-only. A completely separate registrable domain is the strongest isolation. - SELinux: new directories under
/var/www/htmlneedrestorecon(SELinux file contexts and booleans on web servers).
See also
- nginx in front of Apache (TLS proxy on :8008)
- Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects)
- Deploy scripts with rsync (patterns and traps)
- Securing a shared AI agent that other users can drive
- Sandboxing Claude Code for untrusted users (bubblewrap, --restricted, token pipe)