custom 502 error for stacked proxies

Sergey Kandaurov pluknet at nginx.com
Fri May 3 10:47:40 UTC 2019



> On 3 May 2019, at 03:52, Paul B. Henson <henson at acm.org> wrote:
> 
> 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.

Hello,

you may want to try recursive error pages in location / {}
with error_page 502 in @server_b.
See for details: http://nginx.org/r/recursive_error_pages

-- 
Sergey Kandaurov



More information about the nginx mailing list