Prashikshak / API /nginx.conf
Abhisingh-18's picture
Initial commit: Prashikshak - disaster management training platform
9a92a42
Raw
History Blame Contribute Delete
5.73 kB
# ============================================================================
# NGINX.CONF - Load Balancer Configuration
# ============================================================================
events {
worker_connections 2048;
use epoll;
multi_accept on;
}
http {
# ==================== UPSTREAM BACKEND SERVERS ====================
upstream backend {
least_conn; # Route to server with fewest active connections
# Docker Compose creates multiple backend containers
# They all listen on port 3000 internally
server backend:3000 max_fails=3 fail_timeout=30s weight=1;
# Keep connections alive for better performance
keepalive 32;
keepalive_requests 100;
keepalive_timeout 60s;
}
# ==================== BASIC SETTINGS ====================
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Logging
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'upstream: $upstream_addr upstream_status: $upstream_status '
'request_time: $request_time upstream_response_time: $upstream_response_time';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
# Performance
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
# Timeouts
client_body_timeout 12s;
client_header_timeout 12s;
send_timeout 10s;
# Buffer sizes
client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 50m;
large_client_header_buffers 4 16k;
# Gzip compression
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml text/javascript
application/json application/javascript application/xml+rss
application/rss+xml font/truetype font/opentype
application/vnd.ms-fontobject image/svg+xml;
# ==================== MAIN SERVER ====================
server {
listen 80;
server_name _; # Replace with your domain in production
# ==================== HEALTH CHECK (No Auth, No Logging) ====================
location = /api/v1/health {
access_log off;
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
location = /api/v1/ready {
access_log off;
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
# ==================== API ROUTES ====================
location /api/ {
# Proxy settings
proxy_pass http://backend;
proxy_http_version 1.1;
# Headers
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
# Timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# Buffering
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 4k;
proxy_busy_buffers_size 8k;
# Error handling - try next upstream server on errors
proxy_next_upstream error timeout http_502 http_503 http_504;
proxy_next_upstream_tries 2;
# Don't cache by default
proxy_cache_bypass $http_upgrade;
add_header X-Cache-Status $upstream_cache_status;
}
# ==================== ROOT ROUTE ====================
location / {
return 200 '{"message":"API Server Running","status":"ok"}';
add_header Content-Type application/json;
}
# ==================== ERROR PAGES ====================
error_page 502 503 504 /50x.html;
location = /50x.html {
return 503 '{"error":"Service temporarily unavailable","status":503}';
add_header Content-Type application/json;
}
}
}
# ============================================================================
# NOTES:
# ============================================================================
# 1. Docker Compose networking automatically resolves "backend" to all
# backend service replicas. You don't need to list each IP/port manually.
#
# 2. The upstream block uses "least_conn" to route to the server with the
# fewest active connections (better than round-robin for varying load).
#
# 3. Health checks at /api/v1/health don't create access logs (reduces noise).
#
# 4. Error handling with proxy_next_upstream ensures failed requests are
# automatically retried on a different backend server.
#
# 5. For HTTPS in production, add SSL configuration:
#
# server {
# listen 443 ssl http2;
# ssl_certificate /etc/nginx/ssl/cert.pem;
# ssl_certificate_key /etc/nginx/ssl/key.pem;
# ssl_protocols TLSv1.2 TLSv1.3;
# ssl_ciphers HIGH:!aNULL:!MD5;
# # ... rest of config ...
# }
# ============================================================================