# ComerYA · Ejemplo de configuración para Nginx + PHP-FPM 8.3
server {
    listen 80;
    server_name comerya.mx www.comerya.mx;
    return 301 https://comerya.mx$request_uri;
}

server {
    listen 443 ssl http2;
    server_name comerya.mx;
    root /var/www/comerya/public;      # SÓLO el directorio public
    index index.php;

    ssl_certificate     /etc/letsencrypt/live/comerya.mx/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/comerya.mx/privkey.pem;

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

    add_header X-Content-Type-Options nosniff always;
    add_header Referrer-Policy strict-origin-when-cross-origin always;

    location ~ /\. { deny all; }

    # Nunca ejecutar PHP dentro de uploads
    location ^~ /uploads/ {
        location ~ \.(php|phtml|phar|html?|svg|js)$ { deny all; }
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    # El service worker y el manifiesto deben revisarse en cada visita
    location = /sw.js { add_header Cache-Control "no-cache"; try_files $uri =404; }
    location = /manifest.webmanifest { add_header Cache-Control "no-cache"; try_files $uri /index.php?$query_string; }

    location ~* \.(css|js|woff2?|png|jpe?g|webp|avif|ico)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        try_files $uri =404;
    }

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

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

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
}
