server {
    listen 80;
    server_name cloudvault.local; # Change to your actual domain
    
    # Point to the public directory
    root /var/www/cloudvault/public;
    index index.php index.html;

    # Allow large file uploads (up to 5GB)
    client_max_body_size 5G;
    
    # Increase timeouts for large uploads
    client_body_timeout 600s;
    send_timeout 600s;

    # Route all normal requests to index.php
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Internal location for X-Accel-Redirect (Secure File Streaming)
    # The client can NEVER access this URL directly. It can only be triggered via PHP headers.
    location /internal_storage/ {
        internal;
        # Must map to the absolute path of the storage/files directory
        alias /var/www/cloudvault/storage/files/;
    }

    location ~ \.php$ {
        include fastcgi_params;
        # Adjust Unix socket or TCP port based on your PHP-FPM setup
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        
        # Increase FastCGI timeouts to prevent gateway timeouts on upload
        fastcgi_read_timeout 600;
        fastcgi_send_timeout 600;
    }

    # Deny access to hidden files (e.g. .env, .git)
    location ~ /\. {
        deny all;
    }
}
