server {
    listen 80;
    server_name ${SERVER_NAME:-localhost};

    root /var/www/html/public;
    index index.php;

    # Configuration de l'authentification basique pour tout le serveur
    auth_basic "API Mozart Digital";
    auth_basic_user_file /etc/nginx/auth/.htpasswd;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass   api-php:9000;                 # nom du service PHP, port 9000 par défaut
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        
        # Transférer l'en-tête Authorization à PHP
        fastcgi_param HTTP_AUTHORIZATION $http_authorization if_not_empty;
    }
    
    # Ajouter les en-têtes CORS pour permettre l'accès depuis tous les domaines
    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
    add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, Accept' always;
}
