skip to Main Content

Sometimes I need to stop my website to put it under maintenance and I use the following NGINX configuration:

server {
        server_name     mypage.com;
        listen          443 ssl http2;
        listen          [::]:443 ssl http2;

        root            /var/www/mypage.com/public_html/;
        index           maintenance.html;
}

The question is, if I access my website as follows:
https://mypage.com
everything works correctly and the maintenance.html page that I want is displayed.

The problem is when someone accesses a page within my site that is NOT the main one (for example, https://mypage.com/contact), which causes a page not found error.

What I want to achieve is that, in maintenance mode, any visitor of my web, accessing any subpage of it, arrives to the maintenance.html page.

Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    Trying and trying it over and over again... I've found the solution:

    server {
            server_name     mypage.com;
            listen          443 ssl http2;
            listen          [::]:443 ssl http2;
    
            root            /var/www/mypage.com/public_html/;
            index           maintenance.html;
    
            location / {
                    try_files $uri $uri/ /maintenance.html
            }
    }
    

  2. location / {
        try_files maintenance.html
    }
    

    Adding this in inside the server block should do it, it will always match this location unless a more precise one is declared.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search