NGINX: アップストリームから接続が拒否された場合、一定期間 proxy_pass を再試行する

NGINX: アップストリームから接続が拒否された場合、一定期間 proxy_pass を再試行する

私は、アップストリーム FCGI サーバーが接続を受け入れ、正常に応答する場合に正常に動作する次の構成ファイルを使用して NGINX 1.25.3 を実行しています。

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 が最大 20 秒間アップストリーム FCGI サーバーへの接続を試行し続け、その後エラー 502 を返すようにしたいと考えています。

私は次のようなさまざまなバリエーションを試しました:

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

502 エラーを返す前に、指定された期間アップストリーム接続を再試行するように NGINX を設定するには、proxy_next_upstream および proxy_connect_timeout ディレクティブを使用します。以下は、目的の動作を実現する更新された設定です。

このコードを使用してください

アップストリーム backend_nomad_internal2 { サーバー 127.0.0.1:42250; サーバー 127.0.0.1:42251; サーバー 127.0.0.1:42252; }

サーバー { 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;

    # 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 error timeout http_500 http_502 http_503 http_504;: NGINX が次のアップストリーム サーバーを試行する条件を指定します。この場合、エラーとタイムアウトが含まれます。

-> proxy_timeout 20s;: 接続の確立から上流サーバーからの応答の受信までの最大許容時間を設定します。この場合、20 秒に設定されています。

これらの設定により、NGINX は最大 20 秒間アップストリーム サーバーに接続しようとし、必要に応じて再試行してから、クライアントに 502 エラーを返します。特定の要件に基づいてタイムアウトを調整します。

関連情報