Custom maintenance page for Apache

This section discusses how to create a custom maintenance page and how to redirect traffic to it.

The example in this section shows how to modify the following files, which is one way to set up your maintenance page:

  • Apache 2.4: /etc/apache2/sites-available/000-default.conf
  • Apache 2.2: /etc/apache2/sites-available/default (Ubuntu), /etc/httpd/conf/httpd.conf (CentOS)

To redirect traffic to a custom maintenance page:

  1. Update your Apache configuration to do the following:

    • Redirect all traffic to the maintenance page
    • Allowlist certain IPs so an administrator can upgrade the Magento software.

    The following example allowlists 192.0.2.110.

    Add the following at the end of your Apache configuration file:

    RewriteEngine On
    RewriteCond %{REMOTE_ADDR} !^192\.0\.2\.110
    RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
    RewriteCond %{DOCUMENT_ROOT}/maintenance.enable -f
    RewriteCond %{SCRIPT_FILENAME} !maintenance.html
    RewriteRule ^.*$ /maintenance.html [R=503,L]
    ErrorDocument 503 /maintenance.html
    Header Set Cache-Control "max-age=0, no-store"
    
  2. Restart Apache:

    • CentOS: service httpd restart
    • Ubuntu: service apache2 restart
  3. Enter the following command:

    touch <web server docroot>/maintenance.enable
    
  4. Upgrade your system.

  5. Test your site to make sure it functions correctly.

  6. After the upgrade is done, delete maintenance.enable.

Custom maintenance page for nginx

This section discusses how to create a custom maintenance page and how to redirect traffic to it.

To redirect traffic to a custom maintenance page:

  1. Use a text editor to open the nginx configuration file that contains your server block.

  2. Add the following to the server block (server is shown for clarity only; don’t add a second server block).

    The following allowlists IP address 192.0.2.110 and 192.0.2.115 on a system where Magento is installed in /var/www/html/magento2:

    server {
         listen 80;
         set $MAGE_ROOT /var/www/html/magento2;
    
         set $maintenance off;
    
         if (-f $MAGE_ROOT/maintenance.enable) {
             set $maintenance on;
         }
    
         if ($remote_addr ~ (192.0.2.110|192.0.2.115)) {
             set $maintenance off;
         }
    
         if ($maintenance = on) {
             return 503;
         }
    
         location /maintenance {
         }
    
         error_page 503 @maintenance;
    
         location @maintenance {
         root $MAGE_ROOT;
         rewrite ^(.*)$ /maintenance.html break;
     }
    
         include /var/www/html/magento2/nginx.conf;
    }
    
  3. Enter the following command:

    touch <magento_root>/maintenance.enable
    
  4. Reload the nginx configuration:

    service nginx reload
    
  5. Upgrade your system.

  6. Test your site to make sure it functions correctly.

  7. After the upgrade is done, delete or rename maintenance.enable

  8. Reload the nginx configuration:

    service nginx reload