DEV Community

Shane Dowling
Shane Dowling

Posted on • Originally published at Medium on

19

Easy maintenance mode with nginx

So I combined a few solutions I found online to come up with a quick way to set up maintenance mode using nginx. Ideally it shouldn’t happen but in times of emergency in can be good to knock up a quick maintenance page for everyone but your own internal ips.

Nginx Config

So here’s the configuration I use for nginx, it starts with setting the internal ips of your company in the main nginx config file. Then in the actual vhost, you can set it to watch for the existence of a maintenance_on.html and when it exists, throw a 503 and use the maintenance page as your 503 error page.

#### /etc/nginx/nginx.conf
map $remote_addr $internal {
default 0;
127.0.0.1 1; # For local connections
some.other.ip 1; # Internal IP 1
and.another.ip 1; # Internal IP 2
and.another.ip 1; # Internal IP 3
}
#### /etc/nginx/conf.d/vhost.conf
server {
....
location / {
set $maintenance 0;
if (-f /path/to/maintenance/maintenance_on.html) {
set $maintenance 1;
}
if ($internal != 1) {
set $maintenance "${maintenance}1";
}
if ($maintenance = 11) {
return 503;
}
proxy_pass http://127.0.0.1:3000;
}
}
view raw nginx.conf hosted with ❤ by GitHub

How to use

By default I have the file named as maintenance_off.html, so it’s only a simple move operation to turn on maintenance mode. For the content of my maintenance file, I generally use this self-contained static html file to serve out a message to customers.

<!doctype html>
<title>Site Maintenance</title>
<style>
body { text-align: center; padding: 150px; }
h1 { font-size: 50px; }
body { font: 20px Helvetica, sans-serif; color: #333; }
article { display: block; text-align: left; width: 650px; margin: 0 auto; }
a { color: #dc8100; text-decoration: none; }
a:hover { color: #333; text-decoration: none; }
</style>
<article>
<h1>We&rsquo;ll be back soon!</h1>
<div>
<p>Sorry for the inconvenience but we&rsquo;re performing some maintenance at the moment. If you need to you can always <a href="mailto:#">contact us</a>, otherwise we&rsquo;ll be back online shortly!</p>
<p>&mdash; The Team</p>
</div>
</article>
view raw gistfile1.html hosted with ❤ by GitHub

Note : Any sort of conditionals going on on your web server is likely to slow things down by a few milliseconds. Again you’re asking your web server to check against network and the disk per request, then make a decision, so bear this in mind before implementing a solution like this.


Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay