# Cloudflare IPs allowlist — update from https://www.cloudflare.com/ips/
# Include this file or paste IPs directly
# /etc/nginx/cloudflare-ips.conf

# Main server block
server {
    listen 443 ssl http2;
    server_name casinoguidese.com;

    # Cloudflare Origin CA cert (15-year, generated in CF Dashboard)
    ssl_certificate     /etc/ssl/cloudflare/origin.pem;
    ssl_certificate_key /etc/ssl/cloudflare/origin.key;

    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;
    ssl_prefer_server_ciphers off;

    # Only allow Cloudflare IPs (add all from https://www.cloudflare.com/ips-v4 and ips-v6)
    # include /etc/nginx/cloudflare-ips.conf;
    # deny all;

    # Rate limiting for API
    limit_req_zone $binary_remote_addr zone=api:10m rate=100r/m;

    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_types text/plain text/html text/css application/javascript application/json image/svg+xml;

    # Proxy to Node.js
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $http_cf_connecting_ip;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_pragma;
        add_header X-Cache-Status $upstream_cache_status;

        # SSR cache headers — Cloudflare will cache these
        proxy_hide_header Cache-Control;
        add_header Cache-Control "public, max-age=3600, s-maxage=3600";
    }

    # Rate limit API endpoints
    location /api/ {
        limit_req zone=api burst=20 nodelay;
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $http_cf_connecting_ip;
        add_header Cache-Control "no-store";
    }

    # No-cache for redirect routes
    location /go/ {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        add_header Cache-Control "no-store, no-cache";
        add_header X-Robots-Tag "noindex";
    }

    # Long-cache static assets (Cloudflare CDN + local)
    location ~* \.(css|js|woff2|svg|webp|avif|png|jpg|ico)$ {
        proxy_pass http://127.0.0.1:3000;
        expires 1y;
        add_header Cache-Control "public, immutable";
    }
}

# Redirect HTTP to HTTPS (for direct connections, Cloudflare handles this via Always Use HTTPS)
server {
    listen 80;
    server_name casinoguidese.com;
    return 301 https://$host$request_uri;
}
