How can I stop nginx from retrying PUT or POST requests on upstream server timeout?

Solution 1:

Starting in nginx 1.9.13, non-idempotent requests (PUT, POST, etc) are not retried by default. If you can upgrade to this version of later, you can obtain the desired behavior by default.

If for some reason you would like to continue retrying PUT, POST, etc (non-idempotent) requests on 1.9.13 or later, use:

proxy_next_upstream error timeout non_idempotent;

Solution 2:

I know i'm pretty late to the game, but for me this is the top result when searching for this problem, so i wanted to share my solution.

This uses the if directive (with one of the few valid use cases) combined with the custom error handler:

upstream backend {
    server backend1;
    server backend2;
}

server {
    server_name proxy;

    location / {
        error_page 598 = @retry;
        error_page 599 = @no_retry;
        if ($request_method = POST) {
            return 599;
        }
        return 598;
    }

    location @retry {
        proxy_pass http://backend;
    }

    location @no_retry {
        proxy_pass http://backend;
        proxy_next_upstream off;
    }
}

Solution 3:

Please see here for doc: proxy_next_upstream

Please note this is an untested gist

https://gist.github.com/wojons/6154645

Solution 4:

use proxy_method directive

refer to: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_method