custom 502 error for stacked proxies

Paul B. Henson henson at acm.org
Fri May 3 00:52:21 UTC 2019


So, I've got a need for a reverse proxy where first it tries server A;
if it gets a 404 from server A it should try server B, and then just
return whatever happens with server B.

I've got this config so far:

location /_nginx_/ {
        internal;
        root /var/www/localhost/nginx;
}

location / {

        proxy_intercept_errors on;
        error_page 403 /_nginx_/error_403.html;
        error_page 404 = @server_b;
        error_page 405 /_nginx_/error_405.html;
        error_page 500 /_nginx_/error_500.html;
        error_page 502 /_nginx_/error_503.html;
        error_page 503 /_nginx_/error_503.html;
        proxy_pass https://serverA;        
        proxy_redirect http://$host/ /;
        proxy_set_header Host $host;
        proxy_http_version 1.1;
        proxy_connect_timeout 3m;
        proxy_read_timeout 3m;
        proxy_buffers 1024 4k;
}

location @server_b {
        proxy_intercept_errors off;
        proxy_pass https://serverB;
        proxy_redirect http://$host/ /;
        proxy_set_header Host $host;
        proxy_http_version 1.1;
        proxy_connect_timeout 3m;
        proxy_read_timeout 3m;
        proxy_buffers 1024 4k;
}

This seems to work *except* when it fails to connect to server B, in which
case it gives a standard nginx 502 error page rather than a custom page.

I've tried all kinds of things, from setting proxy_intercept_errors on
for the @server_b location and adding error_page configuration like in
the / location, and a bunch of other stuff I can't even remember exactly,
but no matter what I do I always get the stock nginx 502 rather than
the custom error page.

Ideally I'd like to just pass through whatever error comes from B, unless
nginx fails completely to connect to B, in which case I'd like to pass
the local custom error page rather than the default nginx page.

What am I missing?

Thanks much...


More information about the nginx mailing list