server {
    listen 80;
    server_name defder.fr;
    
    # Redirection HTTP vers HTTPS
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name defder.fr;
    
    # Certificats SSL (générés par Certbot)
    ssl_certificate /etc/letsencrypt/live/defder.fr/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/defder.fr/privkey.pem;
    
    # AUTHENTIFICATION
    auth_basic "Zone protégée - Connexion requise";
    auth_basic_user_file /etc/nginx/.htpasswd;
    
    # Reverse proxy vers l'app legacy (port 3000)
    location / {
        proxy_pass http://localhost:3000;
        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;
        proxy_cache_bypass $http_upgrade;
    }
}
