Redirect when file detected (maintenance)

Hi,

I’ve got the nginx config which redirects the user when a path is detected.

error_page 503 502 /public/maintenance.html;
location /public/ {
  alias /var/www/service/public/;
}

location / {
  if (-f /var/www/service/maintenance) {
    return 503;
  }

  proxy_pass		 http://localhost:9000;
}

How can I mimic the same behavior with caddy where I look for a path or even better a file and if detected return a maintenance page?

// David

You can use the file matcher to check for the existence of a file on disk:

@hasMaintenance file /var/www/service/maintenance
respond @hasMaintenance "We're under maintenance!" 503

If you need to emit a 503 error to trigger an HTML page to be rendered, you can use the error directive instead of respond, then handle the error with handle_errors to render the page with file_server:

3 Likes

Thank you so much, works perfect! :pray:t3:

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.