上游服务器响应无效怎么办,Nginx 当上游服务器返回失败时的处理办法

95

b08d1082ca3192fecfba97c12f5a75ee.png

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:

http, server, location

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

配置:error:与服务器建立连接、向其传递请求或读取响应头时出错;

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

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

http_:http_500、http_403 等这样错误

non_idempotent:

off:关闭这个功能

Syntax:

proxy_next_upstream_timeout time;

Default:

proxy_next_upstream_timeout 0;

Context:

http, server, location

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

Syntax:

proxy_next_upstream_tries number;

Default:

proxy_next_upstream_tries 0;

Context:

http, server, location

限制将请求传递到下一个服务器的可能尝试次数。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:

http, server, location

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指令

}

标签:上游,error,server,Nginx,proxy,upstream,http,next,服务器返回

来源: https://www.cnblogs.com/jackey2015/p/10438998.html