Исправила маршрутизацию на неготовую реплику

This commit is contained in:
MacBook
2026-10-16 19:53:09 +03:00
parent c1d62f7f23
commit ddebd3a042
8 changed files with 149 additions and 29 deletions
+43
View File
@@ -0,0 +1,43 @@
#!/bin/sh
set -eu
upstream=/run/nginx-upstream.conf
write_servers() {
if [ "$1" = 1 ]; then
printf '%s\n' 'server api1:8080 resolve max_fails=0;'
fi
if [ "$2" = 1 ]; then
printf '%s\n' 'server api2:8080 resolve max_fails=0;'
fi
}
# With no healthy replica, keep both reachable so API can return its normal
# 401/503 JSON responses instead of Nginx reporting "no live upstreams".
write_servers 1 1 > "$upstream"
monitor() {
sleep 2
while :; do
ready1=0
ready2=0
wget -q -T 2 -O /dev/null http://api1:8080/health/ready && ready1=1 || true
wget -q -T 2 -O /dev/null http://api2:8080/health/ready && ready2=1 || true
if [ "$ready1" = 0 ] && [ "$ready2" = 0 ]; then
ready1=1
ready2=1
fi
next=$(mktemp /run/nginx-upstream.XXXXXX)
write_servers "$ready1" "$ready2" > "$next"
if cmp -s "$next" "$upstream"; then
rm -f "$next"
else
mv "$next" "$upstream"
nginx -t >/dev/null 2>&1 && nginx -s reload
fi
sleep 1
done
}
monitor &
exec nginx -g 'daemon off;'
+2 -3
View File
@@ -8,8 +8,7 @@ http {
upstream messenger_api {
zone messenger_api 64k;
server api1:8080 resolve max_fails=1 fail_timeout=2s;
server api2:8080 resolve max_fails=1 fail_timeout=2s;
include /run/nginx-upstream.conf;
keepalive 16;
}
@@ -25,7 +24,7 @@ http {
proxy_connect_timeout 500ms;
proxy_send_timeout 12s;
proxy_read_timeout 12s;
proxy_next_upstream error timeout http_502 http_504;
proxy_next_upstream error timeout http_502 http_503 http_504;
proxy_next_upstream_tries 2;
}
}