| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- # Nginx configuration for production environment
- # HTTP to HTTPS redirect, SSL/TLS, WebSocket support
- user nginx;
- worker_processes auto;
- error_log /var/log/nginx/error.log warn;
- pid /var/run/nginx.pid;
- events {
- worker_connections 2048;
- use epoll;
- }
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log /var/log/nginx/access.log main;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- keepalive_timeout 65;
- types_hash_max_size 2048;
- client_max_body_size 50M;
- # Hide nginx version
- server_tokens off;
- # 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;
- # Rate limiting (optional, adjust as needed)
- limit_req_zone $binary_remote_addr zone=api_limit:10m rate=100r/m;
- limit_req_zone $binary_remote_addr zone=ws_limit:10m rate=10r/s;
- # Upstream API server
- upstream api {
- server api:3000;
- keepalive 32;
- }
- # HTTP server - redirect to HTTPS
- server {
- listen 80;
- server_name _;
- # Allow Let's Encrypt challenges
- location /.well-known/acme-challenge/ {
- root /var/www/certbot;
- }
- # Redirect all other traffic to HTTPS
- location / {
- return 301 https://$host$request_uri;
- }
- }
- # HTTPS server
- server {
- listen 443 ssl http2;
- server_name _;
- # SSL Configuration
- # Using Let's Encrypt certificates from certbot container
- # DOMAIN_NAME_PLACEHOLDER will be replaced by nginx entrypoint script
- ssl_certificate /etc/letsencrypt/live/DOMAIN_NAME_PLACEHOLDER/fullchain.pem;
- ssl_certificate_key /etc/letsencrypt/live/DOMAIN_NAME_PLACEHOLDER/privkey.pem;
- # SSL Security Settings
- ssl_protocols TLSv1.2 TLSv1.3;
- ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
- ssl_prefer_server_ciphers off;
- ssl_session_cache shared:SSL:10m;
- ssl_session_timeout 10m;
- ssl_session_tickets off;
- # Security Headers
- add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
- add_header X-Frame-Options "DENY" 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;
- # Health check endpoint (no rate limiting)
- location /health {
- proxy_pass http://api/health;
- 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;
-
- access_log off;
- }
- # WebSocket support for Socket.io (with rate limiting)
- location /socket.io/ {
- limit_req zone=ws_limit burst=20 nodelay;
-
- proxy_pass http://api;
- proxy_http_version 1.1;
-
- # WebSocket upgrade headers
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Connection "upgrade";
-
- # Standard proxy headers
- 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;
-
- # WebSocket timeouts (Socket.io uses long-lived connections)
- proxy_read_timeout 86400s;
- proxy_send_timeout 86400s;
- proxy_connect_timeout 60s;
-
- # Disable buffering for real-time data
- proxy_buffering off;
- proxy_cache off;
- }
- # API endpoints (with rate limiting)
- location /api/ {
- limit_req zone=api_limit burst=50 nodelay;
-
- proxy_pass http://api;
- 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;
-
- # Timeout settings
- proxy_connect_timeout 60s;
- proxy_send_timeout 60s;
- proxy_read_timeout 60s;
-
- # Buffer settings
- proxy_buffering on;
- proxy_buffer_size 4k;
- proxy_buffers 8 4k;
- }
- # Root endpoint
- location / {
- proxy_pass http://api;
- 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;
- }
- }
- }
|