Maintenance page with Nginx with specific permitted access

Nginx 503 Error

At the moment, we're in a fairly complicated upgrade of a Magento live store and during the upgrade window, it is necessary to put up a holding page. A simple maintenance.flag file isn't going to cut it for this process, so cutting off users at the web server is more suitable.

For this particular customer, they have a complex 3 server cluster running a single site with over 240,000 unique visitors per day, so Nginx was a more appropriate choice.

Going into maintenance

Putting up a maintenance page with a 503 header is required, but in addition to this, to make sure payment gateway IPNs can function correctly, we need to permit access to some URLs (where we do not know the source IP address), eg. PayPal or Ogone. Also, we'll need to add our own IP address to the allow list.

In the Nginx vhost configuration file:

server {
..
 set $maintenance on;

 if ($remote_addr ~ (my.ip.add.ress|my.second.ipadd.ress|my.third.ipadd.ress)) {
  set $maintenance off;
 }

 if ($uri ~ ^/(index.php/)?(ogone|paypal)/(.*)$ ) {
  set $maintenance off;
 }

 if ($maintenance = on) {
  return 503;
 }

 location /maintenance {
 }

 error_page 503 @maintenance;

 location @maintenance {
  root $maintenance_root;
  rewrite ^(.*)$ /index.html break;
 }
..
}