nginx not serving my error_page

error_page handles errors that are generated by nginx. By default, nginx will return whatever the proxy server returns regardless of http status code.

What you're looking for is proxy_intercept_errors

This directive decides if nginx will intercept responses with HTTP status codes of 400 and higher.

By default all responses will be sent as-is from the proxied server.

If you set this to on then nginx will intercept status codes that are explicitly handled by an error_page directive. Responses with status codes that do not match an error_page directive will be sent as-is from the proxied server.


You can set proxy_intercept_errors especially for that location

location /some/location {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 5;
    proxy_read_timeout 240;
    proxy_pass http://127.0.0.1:4701/;
    proxy_intercept_errors on; # see http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors

    error_page 400 500 404 ... other statuses ... =200 /your/path/for/custom/errors;
}

and you can set instead 200 other status what you need


People who are using FastCGI as their upstream need this parameter turned on

fastcgi_intercept_errors on;

For my PHP application, I am using it in my upstream configuration block

 location ~ .php$ { ## Execute PHP scripts
    fastcgi_pass   php-upstream; 
    fastcgi_intercept_errors on;
    error_page 500 /500.html;
 }