Files
mtusi2026lab3/infra/nginx/nginx.conf
drunkdiente 3aba500a0e Реализовать стенд лабораторной 3: две API-реплики и балансировка
FastAPI + PostgreSQL как единый источник истины: пользователи, Basic/Bearer-сессии, чаты, сообщения. Сессии и идемпотентные ключи хранятся в БД, поэтому реплики взаимозаменяемы. Общая идемпотентность POST сообщения по Idempotency-Key, включая конкурентный повтор через разные реплики. compose.yaml: postgres, one-shot migrate, api1, api2 и nginx как единственная публичная точка входа. Добавлены scripts/stop-one.sh и scripts/restart.sh, student-tests, evidence и заполненный REPORT.md.
2026-11-07 16:04:29 +03:00

71 lines
2.2 KiB
Nginx Configuration File

user nginx;
worker_processes auto;
pid /var/run/nginx.pid;
error_log /dev/stderr warn;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /dev/stdout;
server_tokens off;
client_max_body_size 1m;
sendfile on;
keepalive_timeout 65;
# Two interchangeable API replicas. Passive health checks: one failed
# connection marks a backend unavailable for fail_timeout (5 s).
upstream api_backend {
server api1:8080 max_fails=1 fail_timeout=5s;
server api2:8080 max_fails=1 fail_timeout=5s;
keepalive 16;
}
server {
listen 8080;
server_name _;
# Message creation is the only POST that is safe to retry at the LB:
# the same Idempotency-Key makes the second attempt return the same
# message. This regex location also covers GET history (idempotent).
location ~ ^/api/v1/chats/[^/]+/messages$ {
proxy_pass http://api_backend;
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 1s;
proxy_send_timeout 5s;
proxy_read_timeout 10s;
proxy_next_upstream error timeout http_502 http_503 http_504 non_idempotent;
proxy_next_upstream_tries 2;
}
# Registration, login and chat creation are not retried transparently:
# without an idempotency contract a retry could create a second object.
location / {
proxy_pass http://api_backend;
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 1s;
proxy_send_timeout 5s;
proxy_read_timeout 10s;
proxy_next_upstream error timeout http_502 http_503 http_504;
proxy_next_upstream_tries 2;
}
}
}