upstream ctf_frontend {
    server 127.0.0.1:8100 fail_timeout=5;
}
# HTTPS server
server {
    listen 443 ssl http2;
    server_name ai-ctf-2025.bsidescapetown.co.za bsidescapetown.co.za;

    ssl_certificate /etc/letsencrypt/live/ai-ctf-2025.bsidescapetown.co.za/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/ai-ctf-2025.bsidescapetown.co.za/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


    # Logging
    access_log /var/log/nginx/ctf-frontend-access.log;
    error_log /var/log/nginx/ctf-frontend-error.log;

    # Timeouts and limits
    keepalive_timeout 60;
    client_max_body_size 4G;
    client_body_timeout 70s;
    client_header_timeout 70s;

    # Proxy settings
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://ctf_frontend;

        # Timeouts for long-running requests
        proxy_connect_timeout 300s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
    }

    # Health check endpoint
    location /health {
        proxy_pass http://ctf_frontend/health;
        access_log off;
    }
}


# HTTP to HTTPS redirect
server {
    listen 80;
    server_name ai-ctf-2025.bsidescapetown.co.za bsidescapetown.co.za;
    return 301 https://$host$request_uri;
}