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.

Thanks for reading! I need your attention for a moment.

Did your problem got solved? Did you enjoy this post? If so, consider thanking me on Patreon. Doing this is not free and I'd love you buy me a beer or coffee. If you do that, I might be able to help you if you didn't get your problem solved with this blog post. I know my shit around areas like website design, coding, blogging, digital marketing and SEO so if you want to do business with me in other ways let me know.

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.

3 comments

  1. Davide

    It seems you didn’t close the } in the location @maintenance block.

    Kind regards

    Reply to this comment
  2. Roni Laukkarinen

    You are correct! Poor article editing from me. Thanks for noticing.

    Reply to this comment
  3. Anonymous

    I tried this. Make sure that all redirect URLs and paramaters are in place before using this. The permanent redirect cannot be undone otherwise, and I was unfortunate enough to make this mistake.

    Reply to this comment

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.