nginx重試機制proxy_next_upstream

nginx做爲反向代理服務器,後端RS有多臺服務器,上層經過必定機制保證容錯和負載均衡。html

nginx的重試機制就是容錯的一種nginx

官方連接:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream後端

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_500          # 服務器返回代碼爲500的響應;
http_502          # 服務器返回代碼爲502的響應;
http_503          # 服務器返回代碼爲503的響應;
http_504          # 服務器返回代碼504的響應;
http_403          # 服務器返回代碼爲403的響應;
http_404          # 服務器返回代碼爲404的響應;
http_429          # 服務器返回代碼爲429的響應(1.11.13);
non_idempotent    # 一般,請求與 非冪等 方法(POST,LOCK,PATCH)不傳遞到請求是否已被髮送到上游服務器(1.9.13)的下一個服務器; 啓用此選項顯式容許重試此類請求;
off               # 禁用將請求傳遞給下一個服務器。

下面還有一個參數影響重試次數,0表示不限制。:服務器

Syntax:     proxy_next_upstream_tries number;
Default:    proxy_next_upstream_tries 0;
Context:    http, server, location

舉例以下:app

location / {
      proxy_pass         http://app-proxy;
      proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
      proxy_next_upstream_tries 3;
      proxy_connect_timeout 60s;
      proxy_read_timeout 60s;
      proxy_send_timeout 60s;
      proxy_pass_request_headers      on;
      proxy_set_header   Host             $host:$server_port;
      proxy_set_header   X-Real-IP        $remote_addr;
      proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
      set $domain default;
相關文章
相關標籤/搜索