user www-data;
worker_processes auto;
pid /run/nginx.pid;
error_log /dev/stderr warn;

events {
    worker_connections 1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    access_log    /dev/stdout;

    sendfile        on;
    tcp_nopush      on;
    keepalive_timeout 65;
    server_tokens   off;

    limit_req_zone $binary_remote_addr zone=contact_limit:10m rate=5r/m;

    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_proxied any;
    gzip_types text/plain text/css text/javascript application/javascript
               application/json image/svg+xml font/woff2;

    server {
        listen 80;
        server_name _;
        root /var/www/html;
        index index.html index.php;
        charset utf-8;

        add_header X-Frame-Options "SAMEORIGIN" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Referrer-Policy "strict-origin-when-cross-origin" always;
        add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
        add_header Cross-Origin-Opener-Policy "same-origin" always;
        add_header Cross-Origin-Resource-Policy "same-origin" always;
        add_header Content-Security-Policy "default-src 'self'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; object-src 'none'; img-src 'self' data:; font-src 'self' https://fonts.gstatic.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; script-src 'self' 'unsafe-inline' https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/; frame-src https://www.google.com/recaptcha/; connect-src 'self' https://www.google.com/recaptcha/; upgrade-insecure-requests" always;

        location ~ /\. { deny all; return 404; }
        location ~* \.(env|conf|yml|yaml|md|sh|py|pyc|sql|bak|old|swp|log|dockerfile)$ {
            deny all; return 404;
        }
        location = /Dockerfile { deny all; return 404; }

        location = /robots.txt  { try_files $uri =404; access_log off; }
        location = /sitemap.xml { try_files $uri =404; access_log off; }
        location = /llms.txt    { try_files $uri =404; access_log off; }

        location ~* \.(css|js|woff2|woff|ttf|otf)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
            access_log off;
        }
        location ~* \.(jpg|jpeg|png|webp|gif|ico|svg|mp4|webm)$ {
            expires 6M;
            add_header Cache-Control "public";
            access_log off;
        }

        location = /api/contact {
            limit_req zone=contact_limit burst=3 nodelay;
            limit_req_status 429;
            rewrite ^ /api/contact.php last;
        }
        location = /api/contact/ {
            limit_req zone=contact_limit burst=3 nodelay;
            limit_req_status 429;
            rewrite ^ /api/contact.php last;
        }

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_read_timeout 30s;
        }

        location ~ ^/(?!404\.html$|500\.html$)(.+)\.html$ {
            return 301 /$1/;
        }

        location / {
            try_files $uri $uri/index.html $uri/ =404;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /500.html;
        location = /404.html { internal; }
        location = /500.html { internal; }
    }
}
