Set maintenance page in nginx

Problem

When you need to stop the web application and do some maintenance, sometimes it may take a bit while so you want to show a custom maintenance page

Solution

Config in your application’s configuration in Nginx:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
root /path/to/project;

error_page 503 /maintenance.html;
location = /maintenance.html {
internal;
}

location / {
# if under maintenance
# return 503;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto "https";
proxy_set_header Host $http_host;
proxy_set_header Connection "";
proxy_redirect off;
proxy_pass http://web_server;
}

You just need to put a “maintenance.html” page under the /path/to/project path, and when in maintenance, just comment out the line of return 503;. Then Nginx will return 503 for every request under / and it will map 503 to page “maintenance.html”.

Reference