大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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 undesi…

问题原文

原帖链接 · David Tinker · CC BY-SA 3.0

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;        
    }
}

社区高分解答

原帖链接 · Pavel Evstigneev · CC BY-SA 4.0

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;

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2013-08-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。