error_page redirect sends incomplete request to upstream

Maxim Dounin mdounin at mdounin.ru
Mon Jul 11 14:36:30 UTC 2016


Hello!

On Sun, Jul 10, 2016 at 07:50:36AM +0000, Manole, Sorin wrote:

> Hello,
> 
> I opened https://trac.nginx.org/nginx/ticket/1010 for this.
> 
> The bug appears when request_body_buffering is off (but maybe not only), and error pages are set to be served from an upstream using an internal redirect.
> 
> If a problem happens while the request body is read/sent to the upstream in a non-buffered fashion, and nginx tries to serve an error page, it rewrites the HTTP method from POST to GET request, but keeps the old value of the Content-Length header when trying to serve the error page from the upstream.
> This is done here: https://trac.nginx.org/nginx/browser/nginx/src/http/ngx_http_special_response.c#L575
> No body data is sent in the error page upstream request, even though it is declared in the header, which can cause the upstream server to wait for it. This is seen by the client as the request hanging until the configured upstream timeout.
> 
> Can this be fixed by clearing any Content-Length or Transfer-Encoding headers when error pages are served?

When using error_page with "proxy_request_buffering off" it is not 
possible to re-send a request to upstream as is, as the body is no 
longer available (or only partially available).

If you want to redirect errors after a failed proxy_pass with 
disabled request body bufferng to an upstream server, consider 
using "proxy_pass_request_body off" and clearing 
Content-Length/Transfer-Encoding headers with proxy_set_header, 
e.g.:

    location /upload/ {
        proxy_pass ...
        proxy_request_buffering off;
        error_page 502 /error;
    }

    location /error {
        proxy_pass ...
        proxy_pass_request_body off;
        proxy_set_header Content-Length "";
        proxy_set_header Transfer-Encoding "";
    }

As of now it is not something handled automatically and probably 
won't be in the near future, "proxy_request_buffering off" is a 
special mode and it is to be used with care.  I personally 
wouldn't recommend redirecting any errors after a failed 
proxy_pass with disabled request body buffering to anything but a 
static file (if at all).

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx-devel mailing list