• Servers and Tools

Showing a Maintenance Page

From the class:  Getting Started with NGINX

Let me show you a cool trick. I'm going to put an IF statement inside this location. And the IF statement is going to check whether or not there's a file that exists. And if this file exists-- I'm just going to leave this blank for a second-- instead of executing this, we're going to go ahead and return a 503 status code. That's the maintenance page.

Whenever there's a return statement like this, everything else stops. So this is the last thing that we'll execute. And when Nginx sees this 503 code returned, it's automatically going to route you to the 503.HTML page. So we can use this IF statement to dynamically check whether or not there's some file that exists. And if that file exists, we'll show the maintenance page. And if it doesn't exist, we'll just go and show the regular app.

We can make this file whatever we want. But I'm going to put it into the apps, Boom. I'm going to put it in the temp folder. So it's not going to be in our public folder. I don't want the public to be able to access it. And it's temporary because it's only going to exist for however long we want to be in maintenance mode.

And I'll say go ahead and look for a file named maint.txt. I could say maintenance, but just to make it easier to type. I'll abbreviate it to m-a-i-n-t, maint.txt. So if this file exists, then we must be in maintenance mode. So go ahead and return the 503 code.

I'll save that and we'll restart the server, the Nginx server, and try this out. Navigate into the temp folder of the Boom app. And before we do anything, let's make sure that the regular server is working. I'm going to open up another tab here. And we'll just curl follow redirects

And we should just be getting our regular app at this point. It looks like we are. And so it's working correctly. Now let's go ahead and put a maintenance.txt file in here. We don't even have to put anything in the file. I can just touch it and make an empty one.

And now that I do that, let's curl for again. And look, we get back the maintenance page. So our IF statement worked. And Nginx dynamically served up the maintenance page just based on whether or not this maint.txt file was in our temp folder. If we want to get rid of the maintenance page, we could just remove the maintenance text files. So remove maint.txt.

Got to spell it correctly. And we'll come down on the bottom and try again. And now we're back into our app. So this provides a nice way for us to show a maintenance page simply by putting a text file into this folder.

And you can imagine where we have a script that does this for us automatically and we could execute the script any time we're about to do any kind of maintenance. And when we're done, we can execute another script that removes the file for us on all the Nginx servers automatically.