# /etc/nginx/sites-available/drilharob.ao
server {
    listen 80;
    listen [::]:80;
    server_name drilharob.ao www.drilharob.ao admin.drilharob.ao;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.drilharob.ao;
    ssl_certificate /etc/letsencrypt/live/drilharob.ao/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/drilharob.ao/privkey.pem;
    return 301 https://drilharob.ao$request_uri;
}

# Public site and administration portal share the same application. Laravel decides by host:
# drilharob.ao serves the website (where /admin is 404) and admin.drilharob.ao the portal.
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name drilharob.ao admin.drilharob.ao;
    root /var/www/drilharob/current/public;
    index index.php;
    charset utf-8;

    ssl_certificate /etc/letsencrypt/live/drilharob.ao/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/drilharob.ao/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_session_cache shared:SSL:10m;

    client_max_body_size 64M;

    gzip on;
    gzip_types text/plain text/css application/javascript application/json image/svg+xml application/xml;

    # Hashed build assets and uploaded media: long-lived cache.
    location ~* ^/(build|storage)/.*\.(js|css|webp|avif|jpg|jpeg|png|gif|svg|woff2?|mp4|webm|pdf)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
        try_files $uri =404;
    }

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

    location = /favicon.ico { access_log off; log_not_found off; }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_hide_header X-Powered-By;
    }

    # Never serve dotfiles or PHP from uploads.
    location ~ /\.(?!well-known) { deny all; }
    location ~* ^/storage/.*\.php$ { deny all; }
}
