prepared skeleton for lab5
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
# Единая точка входа: round-robin между API-репликами, без sticky sessions.
|
||||
# Директивы рассчитаны на закреплённый nginx:1.28.0-alpine.
|
||||
|
||||
worker_processes 2;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
# В логе используем request_id ответа API, для собственных ошибок — id nginx.
|
||||
map $upstream_http_x_request_id $response_request_id {
|
||||
"" $request_id;
|
||||
default $upstream_http_x_request_id;
|
||||
}
|
||||
|
||||
log_format requests escape=json
|
||||
'{"time":"$time_iso8601","request_id":"$response_request_id",'
|
||||
'"method":"$request_method","uri":"$uri","status":$status,'
|
||||
'"bytes":$body_bytes_sent,"request_time":$request_time,'
|
||||
'"upstream":"$upstream_addr","upstream_status":"$upstream_status",'
|
||||
'"upstream_time":"$upstream_response_time",'
|
||||
'"instance_id":"$upstream_http_x_instance_id",'
|
||||
'"limit_status":"$limit_req_status"}';
|
||||
|
||||
access_log /dev/stdout requests;
|
||||
error_log /dev/stderr warn;
|
||||
server_tokens off;
|
||||
|
||||
# DNS Docker возвращает адреса всех реплик сервиса api.
|
||||
resolver 127.0.0.11 valid=5s ipv6=off;
|
||||
resolver_timeout 1s;
|
||||
|
||||
# Общий для worker-процессов лимит на IP, который видит nginx.
|
||||
# Клиентский X-Forwarded-For не используется в качестве ключа лимита.
|
||||
limit_req_zone $binary_remote_addr zone=per_client:10m rate=50r/s;
|
||||
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_min_length 1024;
|
||||
gzip_comp_level 3;
|
||||
gzip_types application/json;
|
||||
|
||||
upstream api_backend {
|
||||
zone api_backend 64k;
|
||||
server api:8080 resolve max_fails=1 fail_timeout=5s;
|
||||
|
||||
# Кэш свободных соединений на worker, а не лимит запросов к API.
|
||||
keepalive 16;
|
||||
keepalive_timeout 4s;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
|
||||
client_header_timeout 10s;
|
||||
client_body_timeout 10s;
|
||||
# Размер ограничивает API при чтении, включая chunked-передачу.
|
||||
client_max_body_size 0;
|
||||
client_body_buffer_size 64k;
|
||||
send_timeout 10s;
|
||||
keepalive_timeout 30s;
|
||||
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
proxy_connect_timeout 500ms;
|
||||
proxy_send_timeout 10s;
|
||||
proxy_read_timeout 10s;
|
||||
|
||||
# Пассивное обнаружение отказов; Docker healthcheck сам не управляет LB.
|
||||
# non_idempotent не включён: уже отправленные POST не повторяем.
|
||||
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||
proxy_next_upstream_tries 2;
|
||||
# Окно перехода на другой backend, не общий deadline ответа.
|
||||
proxy_next_upstream_timeout 2s;
|
||||
|
||||
# JSON и заголовки ошибок API передаются без подмены.
|
||||
proxy_intercept_errors off;
|
||||
error_page 429 = @rate_limited;
|
||||
error_page 502 503 504 = @unavailable;
|
||||
|
||||
# Проверка самого LB для healthcheck в compose; не проксируется в API.
|
||||
location = /lb-health {
|
||||
access_log off;
|
||||
default_type text/plain;
|
||||
return 200 "ok\n";
|
||||
}
|
||||
|
||||
# Проверки готовности/жизни не расходуют бюджет бизнес-запросов.
|
||||
location /health/ {
|
||||
proxy_pass http://api_backend;
|
||||
}
|
||||
|
||||
location / {
|
||||
limit_req zone=per_client burst=100 nodelay;
|
||||
limit_req_status 429;
|
||||
limit_req_log_level warn;
|
||||
proxy_pass http://api_backend;
|
||||
}
|
||||
|
||||
location ~ ^/api/v1/chats/[^/]+/attachments$ {
|
||||
limit_req zone=per_client burst=100 nodelay;
|
||||
limit_req_status 429;
|
||||
proxy_request_buffering off;
|
||||
proxy_buffering off;
|
||||
proxy_read_timeout 65s;
|
||||
proxy_send_timeout 15s;
|
||||
# Ошибка одного upload не должна скрыть работающую реплику для чатов.
|
||||
proxy_next_upstream off;
|
||||
proxy_pass http://api_backend;
|
||||
}
|
||||
|
||||
location ~ ^/api/v1/attachments/[^/]+/content$ {
|
||||
limit_req zone=per_client burst=100 nodelay;
|
||||
limit_req_status 429;
|
||||
proxy_buffering off;
|
||||
proxy_read_timeout 65s;
|
||||
proxy_pass http://api_backend;
|
||||
}
|
||||
|
||||
# Это ответы балансировщика: X-Instance-Id реплики у них отсутствует.
|
||||
location @rate_limited {
|
||||
default_type application/json;
|
||||
add_header X-Request-Id $request_id always;
|
||||
add_header Retry-After 1 always;
|
||||
return 429 '{"error":{"code":"rate_limited","message":"Request rate exceeded","request_id":"$request_id"}}';
|
||||
}
|
||||
|
||||
location @unavailable {
|
||||
default_type application/json;
|
||||
add_header X-Request-Id $request_id always;
|
||||
add_header Retry-After 1 always;
|
||||
return 503 '{"error":{"code":"unavailable","message":"API temporarily unavailable","request_id":"$request_id"}}';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user