# nginx front end for the ANBT backend (Phase 2).
# Mounted by docker-compose into the `nginx` service.

server {
    listen 80;
    server_name _;

    # Only public/ is web-exposed. src/, config/, storage/ and .env stay outside
    # the document root.
    root /var/www/html/public;
    index index.php;

    charset utf-8;
    client_max_body_size 10m;

    # Do not advertise the nginx version.
    server_tokens off;

    access_log /dev/stdout;
    error_log  /dev/stderr warn;

    # Health endpoint of the application itself (Application::healthCheck).
    location = /health {
        try_files $uri /index.php$is_args$args;
    }

    # Single front controller.
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT   $document_root;
        fastcgi_read_timeout 60s;
        internal;
    }

    # Refuse any other PHP entry point: /public/foo.php must not be executable.
    location ~ \.php$ {
        return 404;
    }

    # Never serve dotfiles (.env, .git, .htaccess) even if one lands in public/.
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~* \.(jpg|jpeg|png|gif|webp|svg|ico|css|js|woff2?)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
        try_files $uri =404;
    }
}
