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;
    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;

    auth_basic "Review";
    auth_basic_user_file /etc/nginx/.htpasswd;

    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 = /404.html    { try_files $uri =404; access_log off; }
    location = /500.html    { try_files $uri =404; access_log off; }

    location /assets/ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
        try_files $uri =404;
    }

    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 = / { rewrite ^ /api/router.php?type=home last; }
    location = /about { rewrite ^ /api/router.php?type=about last; }
    location = /about/ { rewrite ^ /api/router.php?type=about last; }
    location = /the-practice { rewrite ^ /api/router.php?type=practice last; }
    location = /the-practice/ { rewrite ^ /api/router.php?type=practice last; }
    location = /services { rewrite ^ /api/router.php?type=services_index last; }
    location = /services/ { rewrite ^ /api/router.php?type=services_index last; }
    location ~ ^/services/([a-z0-9-]+)/?$ {
        rewrite ^/services/([a-z0-9-]+)/?$ /api/router.php?type=service&slug=$1 last;
    }
    location = /testimonials { rewrite ^ /api/router.php?type=testimonials last; }
    location = /testimonials/ { rewrite ^ /api/router.php?type=testimonials last; }
    location = /gallery { rewrite ^ /api/router.php?type=gallery last; }
    location = /gallery/ { rewrite ^ /api/router.php?type=gallery last; }
    location = /events { rewrite ^ /api/router.php?type=events last; }
    location = /events/ { rewrite ^ /api/router.php?type=events last; }
    location ~ ^/events/([a-z0-9-]+)/?$ {
        rewrite ^/events/([a-z0-9-]+)/?$ /api/router.php?type=event_detail&slug=$1 last;
    }
    location = /coming-soon { rewrite ^ /api/router.php?type=coming_soon last; }
    location = /coming-soon/ { rewrite ^ /api/router.php?type=coming_soon last; }
    location = /contact { rewrite ^ /api/router.php?type=contact last; }
    location = /contact/ { rewrite ^ /api/router.php?type=contact last; }
    location = /privacy-policy { rewrite ^ /api/router.php?type=legal&slug=privacy-policy last; }
    location = /privacy-policy/ { rewrite ^ /api/router.php?type=legal&slug=privacy-policy last; }
    location = /terms-of-service { rewrite ^ /api/router.php?type=legal&slug=terms-of-service last; }
    location = /terms-of-service/ { rewrite ^ /api/router.php?type=legal&slug=terms-of-service last; }

    location / { return 404; }

    error_page 404 /404.html;
    error_page 500 502 503 504 /500.html;
}
