forked from mtusi2026/mtusi2026lab3
Исправить замечания ревью: ACL при повторе, повтор POST в nginx, активная проверка готовности
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
#!/bin/sh
|
||||
set -u
|
||||
|
||||
SERVERS="${API_UPSTREAMS:-api-1 api-2}"
|
||||
INTERVAL="${HEALTH_INTERVAL:-1}"
|
||||
FALL="${HEALTH_FALL:-2}"
|
||||
RISE="${HEALTH_RISE:-2}"
|
||||
OUT=/etc/nginx/upstream/servers.conf
|
||||
|
||||
mkdir -p "$(dirname "$OUT")"
|
||||
|
||||
var() { echo "$1" | tr -c 'a-zA-Z0-9\n' '_'; }
|
||||
|
||||
render() {
|
||||
any_up=0
|
||||
for s in $SERVERS; do
|
||||
eval "[ \"\$up_$(var "$s")\" = 1 ]" && any_up=1
|
||||
done
|
||||
for s in $SERVERS; do
|
||||
mark=""
|
||||
if [ "$any_up" = 1 ] && eval "[ \"\$up_$(var "$s")\" = 0 ]"; then
|
||||
mark=" down"
|
||||
fi
|
||||
echo "server $s:8080 resolve max_fails=1 fail_timeout=10s$mark;"
|
||||
done
|
||||
}
|
||||
|
||||
for s in $SERVERS; do
|
||||
v=$(var "$s")
|
||||
eval "up_$v=1 ok_$v=0 bad_$v=0"
|
||||
done
|
||||
render > "$OUT"
|
||||
|
||||
watch() {
|
||||
pending=0
|
||||
while sleep "$INTERVAL"; do
|
||||
changed=0
|
||||
for s in $SERVERS; do
|
||||
v=$(var "$s")
|
||||
if wget -q -T 1 -O /dev/null "http://$s:8080/health/ready" 2>/dev/null; then
|
||||
eval "ok_$v=\$((ok_$v + 1)) bad_$v=0"
|
||||
if eval "[ \"\$up_$v\" = 0 ] && [ \"\$ok_$v\" -ge $RISE ]"; then
|
||||
eval "up_$v=1"; changed=1; echo "health: $s ready, back in upstream"
|
||||
fi
|
||||
else
|
||||
eval "bad_$v=\$((bad_$v + 1)) ok_$v=0"
|
||||
if eval "[ \"\$up_$v\" = 1 ] && [ \"\$bad_$v\" -ge $FALL ]"; then
|
||||
eval "up_$v=0"; changed=1; echo "health: $s not ready, marked down"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
if [ "$changed" = 1 ]; then
|
||||
render > "$OUT"
|
||||
pending=1
|
||||
fi
|
||||
if [ "$pending" = 1 ] && nginx -s reload 2>/dev/null; then
|
||||
pending=0
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
watch &
|
||||
exec nginx -g 'daemon off;'
|
||||
+18
-13
@@ -9,8 +9,7 @@ http {
|
||||
|
||||
upstream api {
|
||||
zone api 64k;
|
||||
server api-1:8080 resolve max_fails=3 fail_timeout=10s;
|
||||
server api-2:8080 resolve max_fails=3 fail_timeout=10s;
|
||||
include /etc/nginx/upstream/servers.conf;
|
||||
keepalive 32;
|
||||
keepalive_timeout 4s;
|
||||
}
|
||||
@@ -24,19 +23,25 @@ http {
|
||||
server {
|
||||
listen 8080;
|
||||
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_read_timeout 10s;
|
||||
proxy_send_timeout 5s;
|
||||
proxy_next_upstream_tries 2;
|
||||
proxy_next_upstream_timeout 5s;
|
||||
|
||||
location ~ ^/api/v1/chats/[^/]+/messages$ {
|
||||
proxy_pass http://api;
|
||||
proxy_next_upstream error timeout http_502 http_503 non_idempotent;
|
||||
}
|
||||
|
||||
location / {
|
||||
proxy_pass http://api;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_read_timeout 10s;
|
||||
proxy_send_timeout 5s;
|
||||
proxy_next_upstream error timeout http_502 http_503 non_idempotent;
|
||||
proxy_next_upstream_tries 2;
|
||||
proxy_next_upstream_timeout 5s;
|
||||
proxy_next_upstream error timeout http_502 http_503;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
services:
|
||||
nginx:
|
||||
volumes:
|
||||
- ./infra/scaling/nginx-${REPLICAS:-2}.conf:/etc/nginx/nginx.conf:ro
|
||||
|
||||
api-1:
|
||||
deploy:
|
||||
resources:
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
worker_processes auto;
|
||||
|
||||
events {
|
||||
worker_connections 256;
|
||||
}
|
||||
|
||||
http {
|
||||
resolver 127.0.0.11 valid=5s ipv6=off;
|
||||
|
||||
upstream api {
|
||||
zone api 64k;
|
||||
server api-1:8080 resolve max_fails=3 fail_timeout=10s;
|
||||
keepalive 32;
|
||||
keepalive_timeout 4s;
|
||||
}
|
||||
|
||||
log_format main '$remote_addr - $upstream_addr [$time_local] '
|
||||
'"$request" $status $body_bytes_sent '
|
||||
'"$upstream_response_time" "$request_time"';
|
||||
|
||||
access_log /var/log/nginx/access.log main;
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
|
||||
location / {
|
||||
proxy_pass http://api;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_read_timeout 10s;
|
||||
proxy_send_timeout 5s;
|
||||
proxy_next_upstream error timeout http_502 http_503 non_idempotent;
|
||||
proxy_next_upstream_tries 2;
|
||||
proxy_next_upstream_timeout 5s;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
worker_processes auto;
|
||||
|
||||
events {
|
||||
worker_connections 256;
|
||||
}
|
||||
|
||||
http {
|
||||
resolver 127.0.0.11 valid=5s ipv6=off;
|
||||
|
||||
upstream api {
|
||||
zone api 64k;
|
||||
server api-1:8080 resolve max_fails=3 fail_timeout=10s;
|
||||
server api-2:8080 resolve max_fails=3 fail_timeout=10s;
|
||||
keepalive 32;
|
||||
keepalive_timeout 4s;
|
||||
}
|
||||
|
||||
log_format main '$remote_addr - $upstream_addr [$time_local] '
|
||||
'"$request" $status $body_bytes_sent '
|
||||
'"$upstream_response_time" "$request_time"';
|
||||
|
||||
access_log /var/log/nginx/access.log main;
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
|
||||
location / {
|
||||
proxy_pass http://api;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_read_timeout 10s;
|
||||
proxy_send_timeout 5s;
|
||||
proxy_next_upstream error timeout http_502 http_503 non_idempotent;
|
||||
proxy_next_upstream_tries 2;
|
||||
proxy_next_upstream_timeout 5s;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
worker_processes auto;
|
||||
|
||||
events {
|
||||
worker_connections 256;
|
||||
}
|
||||
|
||||
http {
|
||||
resolver 127.0.0.11 valid=5s ipv6=off;
|
||||
|
||||
upstream api {
|
||||
zone api 64k;
|
||||
server api-1:8080 resolve max_fails=3 fail_timeout=10s;
|
||||
server api-2:8080 resolve max_fails=3 fail_timeout=10s;
|
||||
server api-3:8080 resolve max_fails=3 fail_timeout=10s;
|
||||
server api-4:8080 resolve max_fails=3 fail_timeout=10s;
|
||||
keepalive 32;
|
||||
keepalive_timeout 4s;
|
||||
}
|
||||
|
||||
log_format main '$remote_addr - $upstream_addr [$time_local] '
|
||||
'"$request" $status $body_bytes_sent '
|
||||
'"$upstream_response_time" "$request_time"';
|
||||
|
||||
access_log /var/log/nginx/access.log main;
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
|
||||
location / {
|
||||
proxy_pass http://api;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_read_timeout 10s;
|
||||
proxy_send_timeout 5s;
|
||||
proxy_next_upstream error timeout http_502 http_503 non_idempotent;
|
||||
proxy_next_upstream_tries 2;
|
||||
proxy_next_upstream_timeout 5s;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user