Linux – Trying to add a maintenance page to nginx…. but it doesn’t work

linuxnginx

I have this nginx config for website which has its own web server, that is, nginx is working as a proxy reverse.

Some config parts are omitted:

$ cat /etc/nginx/sites-available/default
server {
    listen 80 default_server;
    [.........];
    return 301 https://$server_name$request_uri;
}

server {
  listen 443 ssl;
  [.........];
  return 301 https://my_website.com$request_uri;
}

server {
    listen 443 ssl default_server;
    [.........];

    location / {
      [.........];
      proxy_redirect  http://localhost:1234 https://my_website.com;
    }

    location /assets/ {
      alias /opt/my_website/assets;
    }

Now I'm trying to add a maintenance page:

    location / {
      [.........];
      proxy_redirect  http://localhost:1234 https://my_website.com;
    }

    location /assets/ {
      alias /opt/my_website/assets;
    }



  # here it is
  if (-f /opt/my_website/maintenance_on.html) {
      return 503;
    }

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

But it simply doesn't work — nothing happens when I create the page.

Best Answer

The rewrite directive produces URIs. You need a root directive to translate it into a local pathname:

location @maintenance {
    root /opt/my_website/html;
    rewrite ^ /maintenance.html break;
}

See this document for details.