form1.cn
Make a little progress every day

nginx中的proxy_next_upstream容错处理

05th of January 2018 Linux Nginx 4583

语法:

proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | non_idempotent | off ...;

默认: proxy_next_upstream error timeout;

上下文: http, server, location

指定请求应传递到下一个服务器的情况:


error:在与服务器建立连接,向其传递请求或读取响应标头时发生错误;


timeout:在与服务器建立连接,向其传递请求或读取响应头时发生超时;


invalid_header:服务器返回空响应或无效响应;


http_500:服务器返回了带有代码500的响应;


http_502:服务器返回具有代码502的响应;


HTTP_503:服务器返回具有代码503的响应;


http_504:服务器返回具有代码504的响应;


http_403:服务器返回带有代码403的响应;


http_404:服务器返回具有代码404的响应;


non_idempotent:通常,如果请求已经被发送到上游服务器(1.9.13),则具有非幂等方法的请求(POST,LOCK,PATCH)不被传递到下一个服务器;启用此选项明确允许重试此类请求;


off:禁用将请求传递到下一个服务器。


运用场景

1、proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;

当其中一台返回错误码404,500...等错误时,可以分配到下一台服务器程序继续处理,提高平台访问成功率,多可运用于前台程序负载,设置proxy_next_upstream

proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;


2、proxy_next_upstream off

因为proxy_next_upstream 默认值: proxy_next_upstream error timeout;

场景:当访问A时,A返回error timeout时,访问会继续分配到下一台服务器处理,就等于一个请求分发到多台服务器,就可能出现多次处理的情况,

如果涉及到充值,就有可能充值多次的情况,这种情况下就要把proxy_next_upstream关掉

proxy_next_upstream off


实例

upstream testweb {
        server 192.168.1.1;
        server 192.168.1.2;
        #ip_hash;
    }
    
server {
        listen       80;
        server_name  127.0.0.1;
        location / {
           proxy_pass http://testweb;
           proxy_headers_hash_max_size 51200;
           proxy_next_upstream http_500 http_502 http_503 http_504 http_404;
           proxy_headers_hash_bucket_size 6400;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header REMOTE-HOST $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

比如当192.168.1.2发生500错误,将不会在轮循该服务器