
업스트림 FCGI 서버가 연결을 수락하고 OK로 응답하는 경우 제대로 작동하는 다음 구성 파일을 사용하여 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가 오류 502를 반환하기 전에 최대 20초 동안 업스트림 FCGI 서버에 계속 연결을 시도하는 것입니다.
나는 다음과 같은 다양한 변형을 시도했습니다.
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 응답을 받습니다.
최대 X초 동안 업스트림 연결을 반복적으로 재시도하고 해당 시간 내에 연결할 수 없는 경우에만 오류 502를 반환하는 방식으로 작동하도록 NGINX를 구성하는 방법이 있습니까?
답변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; }
서버 { 443 SSL 수신; http2 켜짐; 서버_이름 내부2.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는 클라이언트에 502 오류를 반환하기 전에 최대 20초 동안 업스트림 서버에 연결을 시도하고 필요한 경우 다시 시도합니다. 특정 요구 사항에 따라 시간 초과를 조정합니다.