'NGINX config for site down

I'm trying to get nginx to forward to my S3 bucket when my web app k8 pod is down. So I was hoping the below config would, in the case of 50x error or no response, forward request to bucket, then if the document doesn't exist (very likely for first 50x request) - it would then return sitedown.html - this would then request some css files which would fail with same 50x then try on S3 and success.

However it just returns 404 when my application pod is down (if I remove proxy_intercept_errors on; error_page 403 404 =200 I get the S3 404 message as expected).

I want to avoid hosting the website down error page on the nginx server.

Below is my config (everything else config wise is as in the FROM nginxinc/nginx-unprivileged:1.21 docker image)

server {
    listen       8080 default_server;
    server_name  _;
    port_in_redirect off;
    client_max_body_size 51M;
    server_tokens off;

    error_page 501 502 503 504 = @holding_page_proxy;

    location @holding_page_proxy {
        proxy_pass https://tca-holding-pages-permits-dev.s3.eu-west-2.amazonaws.com;
        proxy_intercept_errors on;
        error_page 403 404 =200 https://mybucket.s3.eu-west-2.amazonaws.com/sitedown.html;
    }

    location / {
          proxy_read_timeout 180s;
          proxy_set_header X-Real-IP  $http_x_real_ip;
          proxy_set_header X-Forwarded-Host "";
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_next_upstream error timeout invalid_header http_502 http_503 http_504 http_404;
          proxy_http_version 1.1;
          proxy_pass http://application:8080/;
     }

    # Deny access to the Spring Boot actuator.
    location /actuator {
        deny  all;
    }

    # probe for kubernetes checks
    location = /probe.html {
        root   /usr/share/nginx/html;
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source