NGINX:在連線被上游拒絕時重試 proxy_pass 一段時間

NGINX:在連線被上游拒絕時重試 proxy_pass 一段時間

我正在使用以下設定檔運行 NGINX 1.25.3,只要上游 FCGI 伺服器接受連接並回應正常,該檔案就可以正常工作:

upstream backend_nomad_internal2 {
        # Multiple FCGI listeners defined below
        server 127.0.0.1:42250;
        server 127.0.0.1:42251;
        server 127.0.0.1:42252;
}

server {
        listen 443 ssl;
        http2 on;

        server_name internal2.example.com;

    # BUNCH OF ssl_* CONFIG HERE

        location ~ \.fcgi$ {
                root /usr/share/nginx/html_live/;

                fastcgi_keep_conn on;
                fastcgi_pass backend_nomad_internal2;
                fastcgi_index index.html;
                fastcgi_split_path_info ^(.*cgi)(/.*)$;
                fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
                include fastcgi_params;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html_live/;
        }
}

但是,如果我的上游 FCGI 偵聽器需要重新啟動,它們需要大約 15 秒才能再次開始接受連線。在此視窗期間,NGINX 將立即向客戶端傳回 HTTP 錯誤 502,這是我希望改變的行為。

我希望 NGINX 在返回錯誤 502 之前繼續嘗試連接到上游 FCGI 伺服器長達 20 秒。

我嘗試過多種變體,例如:

proxy_next_upstream error;
proxy_connect_timeout 2s;

proxy_next_upstream error;
proxy_connect_timeout 2s;
proxy_next_upstream_timeout 30s;

error_page 502 503 504 = @retry;
}
location @retry {
    proxy_pass http://backend_nomad_internal2;
    proxy_next_upstream_timeout 30s;
    proxy_next_upstream error;
    proxy_next_upstream_tries 3;  # Adjust the number of retries as needed

    proxy_connect_timeout 2s;
}

但在所有情況下,當上游 FCGI 伺服器拒絕連線時,用戶端都會收到即時 502 回應。

有沒有辦法將 NGINX 配置為重複重試上游連接長達 X 秒,僅在該時間段內無法連接時才返回錯誤 502?

答案1

若要將 NGINX 設定為在傳回 502 錯誤之前在指定的時間內重試上游連接,您可以使用 proxy_next_upstream 和 proxy_connect_timeout 指令。以下是應實現所需​​行為的更新配置:

請使用此程式碼

上游 backend_nomad_internal2 { 伺服器 127.0.0.1:42250;伺服器127.0.0.1:42251;伺服器127.0.0.1:42252; }

伺服器 { 監聽 443 ssl; http2 開啟;伺服器名稱internal2.example.com;

# BUNCH OF ssl_* CONFIG HERE

location ~ \.fcgi$ {
    root /usr/share/nginx/html_live/;
    fastcgi_keep_conn on;

    # Use the following directives to control upstream connection behavior
    proxy_connect_timeout 2s;
    proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    proxy_timeout 20s;

    fastcgi_pass backend_nomad_internal2;
    fastcgi_index index.html;
    fastcgi_split_path_info ^(.*cgi)(/.*)$;
    fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    include fastcgi_params;
}

error_page 500 502 503 504 = @retry;
location @retry {
    # Retry the connection for up to 20 seconds
    proxy_pass http://backend_nomad_internal2;
    proxy_connect_timeout 2s;
    proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    proxy_timeout 20s;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
    root /usr/share/nginx/html_live/;
}

}

在此配置中:

-> proxy_connect_timeout 2s;:設定與上游伺服器建立連線的逾時時間為2秒。

-> proxy_next_upstream 錯誤逾時 http_500 http_502 http_503 http_504;:指定 NGINX 應嘗試下一個上游伺服器的條件。在這種情況下,它包括錯誤和超時。

-> proxy_timeout 20s;:設定建立連線和接收上游伺服器回應之間允許的最大時間。在本例中,它設定為 20 秒。

透過這些設置,NGINX 將嘗試連接到上游伺服器長達 20 秒,並在必要時重試,然後向客戶端返回 502 錯誤。根據您的具體要求調整超時。

相關內容