Last active
March 21, 2021 20:45
-
-
Save guivazcabral/e6e6a7c1e1fc4097fc1474168b12f52e to your computer and use it in GitHub Desktop.
NGINX Maintenance Mode: easy to set and render a template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## THIS IS A WORKAROUND TO SET A GLOBAL VARIABLE | |
## THIS IS WHAT YOU SHOULD CHANGE TO TURN ON MAINTENANCE MODE | |
map $host $maintenance { | |
default false; | |
} | |
## THEN ON THE SERVER BLOCK | |
## IF MAINTENANCE MODE IS TURNED ON | |
## WE RETURN A ERROR CODE | |
## AND RENDER THE APPROPRIATE TEMPLATE | |
if ($maintenance = true){ | |
return 502; | |
} | |
error_page 503 502 @maintenance; | |
## THE REWRITES ARE NEEDED SO THE ASSETS OF THE MAINTENANCE MODE TEMPLATE | |
## ACTUALLY GET SERVED AND NOT RETURNED WITH A 502 ERROR | |
## THIS WAY THEY GET RENDERED, DESPITE THE FACT THAT THEY STILL RETURN 502 | |
## THIS DOESNT WORK WITH .js NOR .css FILES ¯\_(ツ)_/¯ | |
location @maintenance { | |
root /var/www/maintenance; | |
rewrite (\/assets\/image\.png)$ /$1 break; | |
rewrite (\/assets\/image_2\.jpg)$ /$1 break; | |
rewrite ^(.*)$ /index.html break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment