We are using nginx to load balance requests to our application. We have found that nginx switches to a different upstream server when requests time out (good). However it does this for PUT and POST requests which can cause undesirable results (data stored twice). Is it possible to configure nginx to only retry GET requests on timeout? Or is there another way to solve the problem?
Our configuration is as follows:
upstream mash {
ip_hash;
server 127.0.0.1:8081;
server 192.168.0.11:8081;
}
server {
...
location / {
proxy_pass http://mash/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
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: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:
Please see here for doc: proxy_next_upstream
Please note this is an untested gist
https://gist.github.com/wojons/6154645
use
proxy_method
directiverefer to: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_method
I have same problem in my tomcat server. proxy time out when long request occur. i solved my problem by using proxy_read_timeout. when increase timeout then my request never time outed & not occurred any problem. default time out 60s. reference