Nginx 当上游服务器返回失败时的处理办法

95

Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 |http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;
Default:
proxy_next_upstream error timeout;
Context: httpserverlocation

生效前提:没有向客户端发送任何内容 如果向上游服务器发送一个字节说明已经发送生效了 这个指令就失效了

  1. 配置:error:与服务器建立连接、向其传递请求或读取响应头时出错;
  2.  timeout:与服务器建立连接、向其传递请求或读取响应头时发生超时;
  3.  invalid_header:服务器返回空响应或无效响应;
  4.  http_:http_500、http_403 等这样错误
  5.  non_idempotent:
  6.  off:关闭这个功能
Syntax: proxy_next_upstream_timeout time;
Default:
proxy_next_upstream_timeout 0;
Context: httpserverlocation
扫描二维码关注公众号,回复: 6207540 查看本文章

限制将请求传递到下一个服务器的时间。0值关闭此限制

Syntax: proxy_next_upstream_tries number;
Default:
proxy_next_upstream_tries 0;
Context: httpserverlocation

限制将请求传递到下一个服务器的可能尝试次数。0值关闭此限制

代码演示:

server {
        listen       8080;
        server_name  shopp.com.cn;

     

      location /httperr{
           proxy_pass http://nextups;
           proxy_next_upstream http_500; #与下面类同 将错误限定在 500错误的时候 
      }


        location /error {
                proxy_pass http://nextups;
                proxy_connect_timeout 1s;
                proxy_next_upstream error; #当设置为非off得时候,假如一台上游服务器无法链接比如 8012服务器宕机了,那么用户请求会转发到8011端口,但是如果设置为off的话 则返回502错误
    
        }
}    

upstream nextups{
        server 192.168.0.51:8012;
        server 192.168.0.51:8011;
}
Syntax: proxy_intercept_errors on | off; 假如上游服务器发送返回错误码大于300的时候,如果是off则将上游服务器错误信息返回给客户端,如果是on的话 则error_page指令就生效了
Default:
proxy_intercept_errors off;
Context: httpserverlocation
        error_page 500 502 503 504 /test.txt;
         location /intercept {
                root html;
                proxy_pass http://192.168.0.51:8013;
                proxy_intercept_errors on;#当指令启用为on时 则 error_page就生效了 目前指定了文件路径 使用了root指令
                
        }    

猜你喜欢

转载自www.cnblogs.com/jackey2015/p/10438998.html