server {
    listen 80;
    server_name localhost.prestashop-docker.com;

    root /var/www/html/prestashop;
    index index.php index.html;


    # Get real client IP from Kubernetes / Load Balancer
    real_ip_header X-Forwarded-For;
    set_real_ip_from 10.42.0.0/16;
    real_ip_recursive on;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass prestashop:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_param HTTP_AUTHORIZATION $http_authorization;

        # PHP-FPM path info handling (IMPORTANT FOR BACKOFFICE ROUTES)
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;

        fastcgi_intercept_errors on;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
    }

    location ~ /\.ht {
        deny all;
    }

    client_max_body_size 128M;

    location ~ ^/(\d)(?:-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ {
        rewrite ^/(\d)(?:-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ /img/p/$1/$1-home_default.$2 last;
    }

    location ~ ^/(\d)(\d)(?:-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ {
        rewrite ^/(\d)(\d)(?:-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ /img/p/$1/$2/$1$2-home_default.$3 last;
    }

    location ~ ^/(\d)(\d)(\d)(?:-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ {
        rewrite ^/(\d)(\d)(\d)(?:-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ /img/p/$1/$2/$3/$1$2$3-home_default.$4 last;
    }

    location ~ ^/(\d)(\d)(\d)(\d)(?:-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ {
        rewrite ^/(\d)(\d)(\d)(\d)(?:-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ /img/p/$1/$2/$3/$4/$1$2$3$4-home_default.$5 last;
    }

    location ~ ^/(\d)(\d)(\d)(\d)(\d)(?:-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ {
            rewrite ^/(\d)(\d)(\d)(\d)(\d)(?:-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5-home_default.$6 last;
    }

    location ~ ^/(\d)(\d)(\d)(\d)(\d)(\d)(?:-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ {
            rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(?:-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6-home_default.$7 last;
    }

    # Category images
    rewrite ^/c/([\d]+)(-[^/]+)?/.+\.(jpe?g|webp|png|avif)$ /img/c/$1$2.$3 last;
    rewrite ^/c/([a-zA-Z_-]+)(-[\d]+)?/.+\.(jpe?g|webp|png|avif)$ /img/c/$1$2.$3 last;

    # Webservice API
    rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;

    # Fancybox image loader (for old IE)
    rewrite ^/images_ie/([^/]+\.(?:jpe?g|png|gif))$ /js/jquery/plugins/fancybox/images/$1 last;

    # Cache headers for fonts
    location ~* \.(ttf|ttc|otf|eot|woff|woff2|svg)$ {
        add_header Access-Control-Allow-Origin *;
        access_log off;
        log_not_found off;
        expires max;
    }

    location ~ ^/admin[^/]+/index\.php/(.*)$ {
        rewrite ^(/admin[^/]+/index\.php)/(.+)$ $1?$2&$args last;
    }

    # Deny access to certain sensitive files
    location ~* /(composer\.lock|\.git) {
        deny all;
    }

    error_page 404 /index.php?controller=404;
}
