Redirect all traffic to maintenance page except one IP address

It’s pretty amazing that even Stack Overflow failed this time when was googling a snippet for redirecting all traffic to a custom maintenance page, except one IP (mine) with Nginx. When finally found some solutions from page three, noticed it were not enough and composed my solution.

In WordPress, there are plugins for maintenance and on any website, you can code your own solutions. However, using hours for just to display short information about ongoing maintenance and restricting access from everyone but you is just overkill.

Inside server block, add the following snippet and edit to your needs. After the maintenance is over, you can just set $maintenance off, and you’re good to go. Create your maintenance page to yourdomain.com/maintenance.

This snippet is for Nginx. If you still use Apache, unfortunately, no luck here.

server {
    # Here should be the needed server_name and other settings

    # Maintenance mode
    set $maintenance on;

    if ($remote_addr ~ (176.72.246.31|176.72.246.32)) {
      set $maintenance off;
    }

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

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

    if ($maintenance = off) {
      rewrite ^/maintenance(.*)$ https://yourwebsite.com permanent;
    }

    error_page 503 @maintenance;

    location @maintenance {
    rewrite ^(.*)$ /maintenance break;
}

I hope this helps! If you find this useful, I’d be delightful if you left a comment below.

Want to write with me?

Do you know something about tech, nerdy stuff, movies, code, AI, Internet phenomena, social media or anything related to geeky subjects? Join and write with me! I'm currently doing this alone and my time is not sufficient for upkeeping the blog by myself, so all pieces of articles are welcome. Contact me and get your user account.

Roni Laukkarinen

Roni Laukkarinen

Editor-in-chief and owner of Geeky Lifestyle blog. Truly a jack of all trades, a Swiss knife; an avid tech and multimedia geek, coder, owner of a digital agency company, sysadmin, music enthusiast, artist and film critic.

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow The Geek

Geeky Lifestyle is a huge tweeter! Follow @thatgeekyblog to get real-time updates about what's happening in the scene.