Mail-in-a-Box + Web Server (with Clean URLS)
A practical recipe for clean, extensionless URLs on a static site hosted by Mail-in-a-Box nginx, with safe canonical redirects and an optional Google Search Console HTML verification
Quick summary
- Extensionless pages: Serve
/aboutby internally mapping to/about.html(keeps MIME type correct). - Canonical redirects: 301
/*.htmlto the clean URL, but only when the client requested.html(avoid loops). - Verification file (optional): If you use Google Search Console HTML verification, keep that one-off HTML file reachable and un-redirected.
- Two failure modes: A redirect loop on
/and browsers downloading pages due to a wrong MIME type.
Goal
The goal was a single Mail-in-a-Box server that could also host a small static website for the same domain, with clean, extensionless URLs and canonical redirects. The examples below use example.com as a placeholder domain:
Expected behavior (same rules for the apex and www):
Apex (example.com):
https://example.com/aboutloads the About pagehttps://example.com/about.html301 redirects to/abouthttps://example.com/loads the homepagehttps://example.com/index.html301 redirects to/
WWW (www.example.com):
https://www.example.com/aboutloads the About pagehttps://www.example.com/about.html301 redirects to/about
MIAB setup
Start by installing MIAB and getting mail working for your domain. Two details matter for the rest of the steps:
- MIAB owns nginx config: use the domain include hook so changes survive updates.
- Don’t break verification (optional): If you use Google Search Console’s HTML file verification method, you’ll add a one-off verification HTML file yourself (MIAB doesn’t ship it). Keep that exact path reachable and not redirected. I’ll call it
/google123.htmlin this write-up.
Security note: web and mail on one server
You will often see advice to keep a full mail server and a web server on separate machines. That guidance is reasonable for dynamic apps and complex stacks because a web compromise can become a server compromise, which can become an email compromise.
This guide is intentionally narrow: a small static site served by nginx, plus conservative configuration changes, plus keeping mail DNS and protocols direct and unchanged. That does not make the risk zero, but it can be a practical tradeoff when you keep the web surface area small and stay disciplined about patching and monitoring.
- Prefer a static site for the MIAB host. Avoid running a CMS or custom dynamic app on the same box.
- Keep the MIAB admin hostname and mail-related DNS records unproxied and configured exactly as MIAB expects.
- Harden the web edge: consider Cloudflare for WAF and rate limiting, and restrict inbound ports to what you actually need.
- Keep OS and MIAB updates current, and watch logs for abnormal traffic.
Further reading: MIAB forum thread on dynamic sites and MIAB use case discussion.
Primary hostname note (box vs mail)
MIAB expects one primary hostname for admin and system services, commonly box.example.com. You can choose a different subdomain such as mail.example.com, but you must decide during the initial setup. Changing the primary hostname after installation is not supported.
Where customization lives (MIAB)
MIAB’s active domain server block includes a per-domain file. The durable place to put custom directives is typically /home/user-data/www/<your-domain>.conf.
Note: on a fresh MIAB install this file often doesn’t exist yet — you create it, then run MIAB’s web update so nginx picks it up.
Site setup
Put your static site content under /home/user-data/www/default/ (MIAB’s default web root) and verify the basic site loads for both the apex and www before changing any routing behavior.
Clean URL setup
The easiest durable approach is to use MIAB’s per-domain include hook. Your site files still live in /home/user-data/www/default/; this is just nginx behavior.
- (After MIAB setup) Create/edit
/home/user-data/www/<your-domain>.conf - Put this in it:
# /home/user-data/www/<your-domain>.conf
# Keep Google Search Console verification working (no redirect)
location = /google123.html { }
# Serve extensionless pages by internally mapping to .html first.
# This prevents browsers from downloading /about, /projects, etc. due to octet-stream MIME.
location / {
try_files $uri.html $uri $uri/ =404;
}
# Canonicalize only when the CLIENT requested /index.html (avoid internal / -> /index.html)
if ($request_uri = "/index.html") { return 301 /; }
# Canonicalize only when the CLIENT requested a top-level *.html page
if ($request_uri ~ "^/(?!google123\\.html$)(?!index\\.html$)([^/?]+)\\.html(?:\\?.*)?$") {
return 301 /$1$is_args$args;
}
- Run this:
# Apply changes after editing the include
curl -s -d dummy \
--user "$(</var/lib/mailinabox/api.key):" \
http://127.0.0.1:10222/web/update
A couple of notes on why it’s shaped this way:
- The
try_filesorder prefers$uri.htmlso/aboutserves/about.htmland getstext/html. - The redirect conditions use
$request_uriexplicitly so internal index resolution doesn’t cause/to loop. - The regex is intentionally conservative: it only canonicalizes top-level
/*.htmlpages.
Cloudflare setup (hide the origin IP)
After MIAB + the site + clean URLs were stable, the next goal was to stop advertising the server’s public IP for the website. The safe rule with MIAB is: proxy the website, keep mail direct.
- Keep MIAB mail DNS as-is and DNS-only: MIAB needs a full set of records (commonly
MX,A/AAAAformail.example.com, plus variousTXT/SRVrecords for SPF/DKIM/DMARC and client autodiscovery). Do not proxy these; leave them direct so SMTP/IMAP/POP behave normally. - Change only the website hostnames: the only records you should proxy or remove are the web
A/AAAAforexample.comandwww.example.com(details below).
Option A: Cloudflare proxy (simplest)
Proxy only the web records (orange cloud) for example.com and www.example.com and point those A/AAAA records at the server IP. Keep all MIAB-generated mail records exactly as MIAB specifies (DNS-only). Cloudflare becomes the public edge for the website, while nginx remains the origin.
Certificate renewal: MIAB uses certbot on a timer; renewal should continue normally. If you ever hit an edge/origin challenge issue, temporarily disabling proxy for the web hostnames is a straightforward troubleshooting step.
Option B: Cloudflare Tunnel (best at hiding the IP)
A Tunnel keeps the origin IP out of DNS entirely and can eliminate the need for inbound 80/443 from the internet. The Cloudflare edge terminates TLS; the connector forwards requests to local nginx.
With a Tunnel, Cloudflare typically serves example.com and www.example.com via proxied hostnames backed by the Tunnel (often implemented as Cloudflare-managed proxied CNAME records). The key point is the same: keep MIAB mail records untouched, and remove any public A/AAAA records for just the website hostnames.
For a MIAB/nginx web origin, the practical mapping is (HTTPS on the public side, HTTP locally):
# Public hostnames (served as HTTPS at the Cloudflare edge)
example.com -> http://localhost:80
www.example.com -> http://localhost:80
Why HTTP to localhost? It avoids certificate mismatch headaches at the origin because Cloudflare already handles TLS at the edge. nginx still owns clean URLs and canonicalization — Cloudflare is just the transport.
If you want end-to-end TLS: you can point the service at https://localhost:443 instead, but you must ensure the origin certificate and hostname/SNI settings line up.
Bootstrapping certs (what we did)
We temporarily pointed the website DNS records directly at the MIAB server so certbot could issue the initial example.com/www certificates. After issuance, we switched to the Tunnel and removed the public A/AAAA records for example.com and www.example.com only. Cert renewal remains automated via certbot’s systemd timer.
Troubleshooting
Redirect loop on /
If you see ERR_TOO_MANY_REDIRECTS on the homepage, your redirects are probably catching nginx’s internal mapping of / to an index file.
The fix is to only redirect when the client explicitly requested /index.html or /*.html (that’s why the rules below key off $request_uri).
Browser downloads extensionless pages
This usually means a wrong MIME type (often application/octet-stream) for an extensionless path. Serving $uri.html via try_files keeps Content-Type: text/html.
Verification checklist
These are the checks I ran after each change. Using example.com here on purpose:
# Homepage: should be 200
curl -I https://example.com/
# Client-requested index.html: should be 301 to /
curl -I https://example.com/index.html
# Canonical redirect: should be 301 to extensionless
curl -I https://example.com/about.html
# Extensionless route: should be 200 + text/html
curl -I https://example.com/about
# Google verification: should remain 200 (no redirect)
curl -I https://example.com/google123.html
If you want an extra sanity check, validate that nginx is serving extensionless routes as HTML:
curl -sSI https://example.com/about | grep -i '^content-type'
# Expect: content-type: text/html
What I’d remember next time
What the client requested and what nginx ends up serving are not the same thing. The stable combo here was: try_files for extensionless routing, and redirects gated by $request_uri for canonicalization.