Custom 413 error pages when client_max_body_size exceeded

Dan G. Switzer, II dan.switzer at givainc.com
Thu Oct 20 13:04:31 UTC 2022


I'm using nginx/1.20.1 under CentOS Linux release 7.9.2009 (Core) and I 
cannot get a custom error page to show when the client_max_body_size 
limit has been exceeded. The browser will only return the default nginx 
error page.

I see a number of posts mentioning how there was a bug in earlier 
versions of nginx, but this appears to be have been fixed a long time ago.

In my code, I have the following:

> error_page 404 =404 /404_status_code.htm;
> error_page 403 =404 /404_status_code.htm;
>
> location /404_status_code.htm {
>     internal;
>     root /path/to/my/custom/errors/;
>     add_header X-Original-URL "$scheme://$http_host$request_uri" always;
> }

This works perfectly fine. When either a 403 or 404 error is generated, 
nginx returns my custom error page.

However, if I change the code to:

> error_page 404 =404 /404_status_code.htm;
> error_page 403 =404 /404_status_code.htm;
> error_page 413 =413 /413_request_too_large.htm;
>
> location /404_status_code.htm {
>     internal;
>     root /path/to/my/custom/errors/;
>     add_header X-Original-URL "$scheme://$http_host$request_uri" always;
> }
>
> location /413_request_too_large.htm {
>     internal;
>     root /path/to/my/custom/errors/;
>     add_header X-Original-URL "$scheme://$http_host$request_uri" always;
> }

When I try to upload a file larger than my client_max_body_size setting, 
I still get the default error page. I've tried a lot of different 
variations of the code, but nothing seems to work.

I've tried:

> error_page 413 /413_request_too_large.htm;
> location /413_request_too_large.htm {
>     internal;
>     root /path/to/my/custom/errors/;
>     add_header X-Original-URL "$scheme://$http_host$request_uri" always;
> }

Using an handler instead:

> error_page 413 @413_request_too_large
> location @413_request_too_large {
>     internal;
>     root /path/to/my/custom/errors/;
>     add_header X-Original-URL "$scheme://$http_host$request_uri" always;
> }

And every variation I can think of, but nothing seems to work.

Is there something special that needs to be done to implement a custom 
error page for a 413 status code? Or is there perhaps a regression that 
broke this from working?

-Dan

-- 
Dan G. Switzer, II
Giva, Inc.
Email:dan.switzer at givainc.com
Web Site:http://www.givainc.com

See Our Customer Successes
http://www.givainc.com/customers-casestudies.htm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20221020/24d8109e/attachment.htm>


More information about the nginx mailing list