Files
mtusi2026lab6-fork/infra/nginx/nginx.conf
T
2026-11-22 19:55:50 +03:00

125 lines
3.7 KiB
Nginx Configuration File

worker_processes 2;
events {
worker_connections 1024;
}
http {
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;
resolver 127.0.0.11 valid=5s ipv6=off;
resolver_timeout 1s;
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;
keepalive 16;
keepalive_timeout 4s;
}
server {
listen 8080;
client_header_timeout 10s;
client_body_timeout 10s;
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;
proxy_next_upstream error timeout http_502 http_503 http_504;
proxy_next_upstream_tries 2;
proxy_next_upstream_timeout 2s;
proxy_intercept_errors off;
error_page 429 = @rate_limited;
error_page 502 503 504 = @unavailable;
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;
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;
}
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"}}';
}
}
}