# Main context user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; # Load modules load_module modules/ngx_http_geoip_module.so; events { worker_connections 4096; use epoll; multi_accept on; } http { # Basic settings include /etc/nginx/mime.types; default_type application/octet-stream; # Logging format log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' 'rt=$request_time uct="$upstream_connect_time" ' 'uht="$upstream_header_time" urt="$upstream_response_time"'; log_format json escape=json '{' '"time":"$time_iso8601",' '"remote_addr":"$remote_addr",' '"request_method":"$request_method",' '"request_uri":"$request_uri",' '"status":$status,' '"body_bytes_sent":$body_bytes_sent,' '"request_time":$request_time,' '"upstream_response_time":"$upstream_response_time",' '"upstream_addr":"$upstream_addr",' '"http_user_agent":"$http_user_agent",' '"http_x_forwarded_for":"$http_x_forwarded_for",' '"http_authorization":"$http_authorization",' '"upstream_status":"$upstream_status"' '}'; access_log /var/log/nginx/access.log json; # Performance optimizations sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; server_tokens off; client_max_body_size 10M; # 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; gzip_disable "msie6"; # Rate limiting zones limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s; limit_req_zone $http_authorization zone=auth_limit:10m rate=5r/s; limit_conn_zone $binary_remote_addr zone=conn_limit:10m; # Upstream services for CFN Loop upstream orchestrator_backend { least_conn; server orchestrator:3000 weight=3 max_fails=3 fail_timeout=30s; keepalive 32; keepalive_requests 100; keepalive_timeout 60s; } upstream grafana_backend { least_conn; server grafana:3000 weight=3 max_fails=3 fail_timeout=30s; keepalive 32; keepalive_requests 100; keepalive_timeout 60s; } upstream prometheus_backend { least_conn; server prometheus:9090 weight=3 max_fails=3 fail_timeout=30s; keepalive 32; keepalive_requests 100; keepalive_timeout 60s; } # Cache configuration proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=api_cache:10m max_size=1g inactive=60m use_temp_path=off; # Map for authentication bypass map $uri $auth_bypass { default 0; /health 1; /metrics 1; /api/v1/health-scores 1; /api/v1/circuit-breaker 1; } # Server block for HTTP (redirect to HTTPS) server { listen 80; listen [::]:80; server_name _; # Redirect all HTTP traffic to HTTPS return 301 https://$host:443$request_uri; } # Main server block for HTTPS server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name _; # SSL configuration ssl_certificate /etc/nginx/ssl/test.crt; ssl_certificate_key /etc/nginx/ssl/test.key; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; ssl_stapling on; ssl_stapling_verify on; # Security headers add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; 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 "no-referrer-when-downgrade" always; add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self' ws: wss:; frame-ancestors 'none';" always; # Rate limiting limit_req zone=api_limit burst=20 nodelay; limit_conn conn_limit 10; # CORS headers for API endpoints location /api/ { # CORS configuration add_header Access-Control-Allow-Origin "https://app.example.com" always; add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always; add_header Access-Control-Allow-Headers "Authorization, Content-Type, X-Requested-With" always; add_header Access-Control-Max-Age "3600" always; add_header Access-Control-Allow-Credentials "true" always; # Handle preflight requests if ($request_method = 'OPTIONS') { return 204; } # Rate limiting for authenticated requests limit_req zone=auth_limit burst=10 nodelay; # Authentication check (bypass for health endpoints) if ($auth_bypass = 0) { auth_request /auth; auth_request_set $auth_status $upstream_status; } # Proxy to orchestrator proxy_pass http://orchestrator_backend; proxy_http_version 1.1; 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 Connection ""; proxy_set_header X-Gateway-Request-ID $request_id; # 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; # Caching for GET requests proxy_cache api_cache; proxy_cache_key "$scheme$request_method$host$request_uri"; proxy_cache_valid 200 5m; proxy_cache_valid 404 1m; proxy_cache_bypass $http_cache_control $http_authorization; add_header X-Cache-Status $upstream_cache_status; # Error handling proxy_intercept_errors on; error_page 502 503 504 /50x.html; } # Grafana Dashboard location /grafana/ { proxy_pass http://grafana_backend/; proxy_http_version 1.1; 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 Connection ""; # WebSocket support for Grafana live features proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } # Prometheus metrics location /prometheus/ { # IP restriction for security allow 172.30.0.0/16; # cfn-network allow 172.31.0.0/16; # mcp-network allow 127.0.0.1; deny all; proxy_pass http://prometheus_backend/; proxy_http_version 1.1; 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 Connection ""; } # Health check endpoint location /health { access_log off; add_header Content-Type text/plain; return 200 "healthy\n"; } # Metrics endpoint for monitoring location /metrics { stub_status on; access_log off; allow 172.30.0.0/16; # cfn-network allow 172.31.0.0/16; # mcp-network allow 127.0.0.1; deny all; } # Authentication endpoint (internal) location = /auth { internal; proxy_pass http://orchestrator_backend/api/v1/auth/verify; proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header X-Original-URI $request_uri; proxy_set_header X-Gateway-Auth-Request-ID $request_id; } # API test endpoints for testing location /api/v1/test { add_header Content-Type application/json; return 200 '{"status":"ok","message":"API gateway test endpoint","timestamp":"$time_iso8601"}'; } location /api/v1/health-scores { add_header Content-Type application/json; return 200 '{"status":"healthy","scores":{"gateway":1.0,"orchestrator":0.95,"agents":0.88}}'; } location /api/v1/circuit-breaker { add_header Content-Type application/json; return 200 '{"status":"closed","failures":0,"last_failure":null,"reset_timeout":30000}'; } # Error pages location = /50x.html { root /usr/share/nginx/html; add_header Content-Type text/html; return 502 '
The API gateway is currently experiencing issues. Please try again later.
'; } # Default location for unmatched routes location / { add_header Content-Type application/json; return 404 '{"error":"Not Found","message":"The requested endpoint was not found","path":"$uri"}'; } } # Additional server block for direct service access (internal) server { listen 8080; listen [::]:8080; server_name _; # Internal access only allow 172.30.0.0/16; # cfn-network allow 172.31.0.0/16; # mcp-network allow 127.0.0.1; deny all; # Direct access to orchestrator location /orchestrator/ { proxy_pass http://orchestrator_backend/; proxy_http_version 1.1; 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 Connection ""; } # Direct access to Grafana location /grafana-direct/ { proxy_pass http://grafana_backend/; proxy_http_version 1.1; 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 Connection ""; } } }