# Nginx Load Balancer Configuration for lokistack Production # Simple round-robin load balancing between two app instances events { worker_connections 1024; } http { # Upstream for lokistack app instances # ⚠️ 只保留一个后端:平台是 JSON 文件 DB(单写者), # 多实例各挂各的数据卷会互相覆盖数据,不是高可用。 # 详见 docker-compose.prod.yml 顶部说明。 upstream lokistack_backend { server lokistack:8080 max_fails=3 fail_timeout=30s; # Health check for upstream servers (nginx-plus) or passive checks keepalive 32; } # Rate limiting zone limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s; limit_req_zone $binary_remote_addr zone=auth_limit:10m rate=5r/m; # Logging format log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" rt=$request_time'; access_log /var/log/nginx/access.log main; error_log /var/log/nginx/error.log warn; # Security headers add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Content-Type-Options "nosniff" always; add_header X-XSS-Protection "1; mode=block" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; # Gzip compression gzip on; gzip_types text/plain text/css application/json application/javascript; server { listen 8080; server_name _; # Health check endpoint for nginx itself location /health { access_log off; return 200 "healthy\n"; add_header Content-Type text/plain; } # Stricter rate limit for authentication location /api/v1/auth/login { limit_req zone=auth_limit burst=3 nodelay; proxy_pass http://lokistack_backend; 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_set_header X-Forwarded-Proto $scheme; proxy_connect_timeout 5s; proxy_send_timeout 10s; proxy_read_timeout 30s; } # General API rate limiting location /api/ { limit_req zone=api_limit burst=20 nodelay; proxy_pass http://lokistack_backend; 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_set_header X-Forwarded-Proto $scheme; proxy_connect_timeout 5s; proxy_send_timeout 10s; proxy_read_timeout 30s; } # Static files and all other routes location / { proxy_pass http://lokistack_backend; 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_set_header X-Forwarded-Proto $scheme; proxy_connect_timeout 5s; proxy_send_timeout 10s; proxy_read_timeout 30s; proxy_http_version 1.1; proxy_set_header Connection ""; } } }