# =============================================================================
# Nginx vhost for Vihar Sewa on kreonAI5.
#
# Place this file at: /www/server/panel/vhost/nginx/vihar.conf
#                or:   /etc/nginx/sites-available/vihar.conf
#
# Replace <DOMAIN> with your actual subdomain (e.g., vihar.sanjaytextiles.com).
# SSL: aaPanel can issue a Let's Encrypt cert via the panel UI - point it at
#      this server_name and it will fill in the ssl_* lines automatically.
# =============================================================================

# Redirect HTTP -> HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name vsg.kreonsolutions.in;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name vsg.kreonsolutions.in;

    # ---- SSL (filled by aaPanel/certbot) ----
    ssl_certificate     /www/server/panel/vhost/cert/vihar/fullchain.pem;
    ssl_certificate_key /www/server/panel/vhost/cert/vihar/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
    ssl_prefer_server_ciphers on;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;

    # ---- Security headers ----
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Content-Type-Options    "nosniff" always;
    add_header X-Frame-Options           "SAMEORIGIN" always;
    add_header Referrer-Policy           "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy        "geolocation=(self), microphone=(), camera=()" always;

    # ---- Body / buffer ----
    client_max_body_size 4m;
    client_body_buffer_size 128k;

    # ---- Logging ----
    access_log /var/log/nginx/vihar-access.log;
    error_log  /var/log/nginx/vihar-error.log warn;

    # ---- Service worker - never cache ----
    location = /sw.js {
        proxy_pass http://127.0.0.1:3001;
        proxy_set_header Host $host;
        add_header Cache-Control "public, max-age=0, must-revalidate";
    }

    # ---- PWA manifest + icons - long cache ----
    location ~* ^/(manifest\.json|icon-.*\.png|favicon\.ico)$ {
        proxy_pass http://127.0.0.1:3001;
        proxy_set_header Host $host;
        expires 7d;
        add_header Cache-Control "public, max-age=604800";
    }

    # ---- Next.js static assets - immutable cache ----
    location /_next/static/ {
        proxy_pass http://127.0.0.1:3001;
        proxy_set_header Host $host;
        expires 365d;
        add_header Cache-Control "public, max-age=31536000, immutable";
    }

    # ---- API ----
    location /api/ {
        proxy_pass http://127.0.0.1:4001;
        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;

        # Cookies must pass through unchanged
        proxy_pass_request_headers on;

        # Reasonable timeouts
        proxy_connect_timeout 10s;
        proxy_send_timeout    30s;
        proxy_read_timeout    30s;

        # Don't cache API
        add_header Cache-Control "no-store, no-cache, must-revalidate" always;
    }

    # ---- Health check (bypasses /api prefix) ----
    location = /health {
        proxy_pass http://127.0.0.1:4001;
        access_log off;
    }
    location = /health/ready {
        proxy_pass http://127.0.0.1:4001;
        access_log off;
    }

    # ---- Block direct DB / config probes ----
    location ~ /\.(env|git|ht|svn) {
        deny all;
        return 404;
    }

    # ---- Everything else -> Next.js ----
    location / {
        proxy_pass http://127.0.0.1:3001;
        proxy_http_version 1.1;
        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;
    }
}
