Deploys break things. A migration runs long, a file upload stalls halfway, or a config change takes the site down for two minutes while visitors stare at a browser error. A maintenance page in Nginx turns that window into a clear message instead of a broken site: a short page that says the site is down for planned work and will be back soon. This guide sets one up with a flag file you can switch on and off in seconds, with no config reload needed. You need sudo access and a site block you can edit; the examples use Ubuntu paths and a site at /var/www/example.com.
What a Maintenance Page Does During a Deploy
A maintenance page is a small static page Nginx serves with a 503 status code while your real site is unavailable. The 503 status matters: it tells browsers and search engines the outage is temporary, so Google does not treat the downtime as a dead site or drop the page from its index. A page that returns a 200 status with “we are down” text can get indexed as your homepage content.
The page itself should be simple: your site name, one sentence explaining the downtime, and a rough estimate of when the site returns. Keep it to a single HTML file with inline CSS. External stylesheets, images, and fonts live on the same server that is down, so anything the page loads from your site will fail too. It also helps when a release fails: the page stays up while you restore the previous version. For a broader safety net, read the checklist for what to do in the first 10 minutes after your VPS goes down.
How a Maintenance Page in Nginx Actually Works
The mechanism has three parts. First, a flag file on disk, usually named .maintenance, signals that maintenance mode is on. Second, Nginx checks for that file on every request and returns a 503 when it exists. Third, the error_page directive points that 503 at your maintenance page. This is nginx maintenance mode done with a flag file: touch it and the page appears, delete it and the site comes back, all without reloading Nginx.
There is a second way to trigger the page: let Nginx serve it when your upstream fails. If your app runs behind PHP-FPM or a proxy, a 502 or 503 from the backend can point at the same page. The flag file is the better primary trigger because it works even when the backend is healthy, which is exactly the situation during a planned deploy.
Build the Maintenance Page
Create /var/www/example.com/maintenance.html with short markup and inline styling:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8">
<title>We'll be right back | Example</title>
<style>body{font-family:system-ui,sans-serif;text-align:center;
padding:15vh 1rem;color:#222}h1{font-size:1.8rem}</style></head>
<body><h1>We'll be right back</h1>
<p>We're deploying an update. The site should be back within
15 minutes. Please check again shortly.</p></body></html>
Skip a specific return time unless you are sure of it; a vague estimate that turns out right beats a precise one that turns out wrong. Then set ownership so Nginx can read it: sudo chown www-data:www-data /var/www/example.com/maintenance.html && sudo chmod 644 /var/www/example.com/maintenance.html.
Wire Nginx to Serve It
In your site’s server block (typically /etc/nginx/sites-available/example.com), add this inside the server block, before location /:
server {
listen 443 ssl;
server_name example.com;
root /var/www/example.com;
error_page 503 /maintenance.html;
location = /maintenance.html {
root /var/www/example.com;
internal;
}
if (-f $document_root/.maintenance) {
return 503;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
Three details do the heavy lifting. The nginx error_page directive tells Nginx which page to serve for a 503 response, which is the core of the nginx 503 error page pattern; add 502 to it (error_page 502 503 /maintenance.html;) and a crashed PHP-FPM pool shows the same page instead of Nginx’s default 502 error. The location = /maintenance.html block serves that file, and internal means visitors cannot reach it directly by typing the URL. The if check looks for the flag file on each request and returns 503 when it finds it. Nginx documentation warns that if inside a location block can behave unpredictably, but this check sits in the server context and only tests for a file, then returns a status code. If your site already has a detailed server block layout from the Nginx server blocks guide, add these lines near the top of the server block so they run before any location matching.
Before reloading, run sudo nginx -t. A syntax error in a server block takes every site on the server down, so only reload when the test reports success. The guide to testing a new Nginx config without taking your site down walks through the full check.
A Deploy Script That Flips the Page On and Off
A deploy maintenance page you can toggle from a script should turn on, run the deploy, and turn off again, even if a step fails. Without that last part, one failed deploy leaves the flag file behind and the site stays in maintenance mode until someone notices.
#!/bin/bash
set -e
SITE_ROOT="/var/www/example.com"
FLAG="$SITE_ROOT/.maintenance"
trap 'rm -f "$FLAG"' EXIT
touch "$FLAG"
# --- deploy steps go here ---
# git pull, composer install, migrations, cache clear
rm -f "$FLAG"
The trap line is the part that saves you at 2 a.m. If a migration fails and set -e stops the script, the trap still runs and removes the flag. Without it, the deploy fails and the site stays down behind the maintenance page. The script never reloads Nginx: the if check reads the flag file on every request, so the page appears the moment you touch the file and disappears the moment you delete it. Keep deploys short while the page is up; run slow steps first, then flip the page on only for the final switch-over.
Let Yourself Through: IP Bypass for the Deployer
Sometimes you need to check the new version yourself while visitors still see the maintenance page. An IP bypass lets your address through and keeps everyone else on the maintenance page. Add a geo block outside the server block, then adjust the flag check:
geo $maintenance_off {
default 0;
203.0.113.10 1; # your office or home IP
}
if (-f $document_root/.maintenance) {
set $maintenance on;
}
if ($maintenance_off = 1) {
set $maintenance off;
}
if ($maintenance = on) {
return 503;
}
Replace 203.0.113.10 with your real public IP, which you can find by running curl ifconfig.me from your own machine. If your IP changes often, a VPN with a fixed exit IP makes the bypass stable. Do not bypass by user agent or a secret query string: both are easy for a visitor to copy. Test the bypass from a phone on mobile data to confirm strangers still see the maintenance page while your IP gets the live site.
Test It Before Your Next Deploy
Flip the page on and check three things. First, the status code: run curl -I https://example.com/ and confirm the response is HTTP/2 503, not 200. Second, the page itself: open the site in a browser and confirm it renders, with no broken images or unstyled text. Third, cache behavior: if you use Cloudflare, a CDN, or Nginx FastCGI cache, confirm the 503 is not cached as the site’s normal response, or purge the cache after the deploy. Then remove the flag and confirm the site returns a 200.
Common Problems and Fixes
The maintenance page never appears. Check the flag file path first: $document_root must match the root in your server block, and the file must be named exactly .maintenance. Then check that the if block sits in the server context, not inside a location block, and that you reloaded Nginx after editing the config.
The page appears but returns a 200 status. The error_page directive is missing or points at the wrong file, so Nginx serves the static file directly instead of treating it as an error response.
The site stays in maintenance mode after a deploy. A failed deploy left the flag file behind. Check for .maintenance in the site root and delete it. If this happens more than once, add the trap from the deploy script section so a failed step can never strand the flag. The same error_page pattern covers other statuses too: the guide to Nginx error pages for custom 404 and 502 pages shows how to extend it.
A maintenance page in Nginx is a small piece of config with an outsized effect on how a deploy feels. Visitors get a clear message and a reason to come back, search engines get the correct 503 signal, and you get a deploy process that fails safe instead of failing public. Set it up once and test it with a real deploy.