0

I've read a lot of questions about configuring nginx maintenance pages, but I can't figure out how to do this particular thing. Normally my nginx is just proxying everything to another service. For maintenance mode, I need it to return a maintenance.html page, but that page has images and css links. So the problem is if I 503 based on the existence of a file, it 503s all the images too. I can see how to avoid that without the proxy, but when not in maintenance mode, I need the images proxied too and I can't figure out how to do it. Essentially, I need this:

if maintenance mode if image or css -> send /var/www/html/image else -> send /var/www/html/maintenance.html else proxy everything including images and css end 

The other answers I've read assume you want to always serve the images out of /var/www/html, but I only want to do that in maintenance mode. I've gotten this far, I just need to know how to handle the images.

 error_page 503 @maintenance; location @maintenance { ### what goes here to prevent images from 503ing or returning maintenance.html rewrite ^(.*)$ /maintenance.html break; } location / { if (-f /var/www/html/maintenance.html) { return 503; } proxy_pass... } 

Thanks in advance.

6
  • Replace the rewrite statement with try_files $uri /maintenance.html; Commented Nov 15, 2024 at 8:05
  • Thanks, but that just returns a 503 for everything, including the maintenance.html page when in maintenance mode. Commented Nov 15, 2024 at 14:59
  • 1
    Replace the rewrite statement with try_files $uri /maintenance.html =404; (the final =404 is not reached, but allows the /maintenance.html term to be processed within the current location). Also, change the error_page statement to error_page 503 = @maintenance; which allows the response code to be changed from 503 to 200. Commented Nov 15, 2024 at 17:23
  • That did it, thanks.I had to change the image and css paths to be absolute (/style.css instead of style.css) to get nested folders to work correctly, but the config did the trick. I'll update the question with the full solution for future use. Commented Nov 15, 2024 at 22:20
  • I noticed that this also works: "try_files $uri /maintenance.html /;" or "try_files $uri /maintenance.html /whatever;" But I don't understand why that's different than "try_files $uri /maintenance.html;". Can you please explain what's going on? Commented Nov 16, 2024 at 0:04

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.