user  nginx;
worker_processes auto;

events {
    worker_connections 1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;

    # 🔥 Sécurité basique
    server_tokens off;

    # 🔥 Logs (Coolify les capte automatiquement)
    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log warn;

    # ===============================
    # 🔁 HTTP → HTTPS (obligatoire)
    # ===============================
    server {
        listen 80;
        server_name example.com www.example.com;

        return 301 https://$host$request_uri;
    }

    # ===============================
    # 🔐 HTTPS – Django production
    # ===============================
    server {
        listen 443 ssl http2;
        server_name example.com www.example.com;

        # 🔥 Certificats (gérés par Coolify / Let's Encrypt)
        ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

        # 🔥 Sécurité SSL minimale
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;

        # ===============================
        # 📦 Fichiers statiques
        # ===============================
        location /static/ {
            alias /static/;
            expires 30d;
            access_log off;
        }

        # ===============================
        # 🖼️ Médias uploadés
        # ===============================
        location /media/ {
            alias /media/;
            expires 30d;
            access_log off;
        }

        # ===============================
        # 🧠 Django via Gunicorn
        # ===============================
        location / {
            proxy_pass http://web:8000;

            # 🔥 Headers indispensables pour Django
            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 https;

            proxy_redirect off;
        }
    }
}
