nginxurl-rewritingkirby

Nginx rewrite not working (Kirby CMS Cachebuster)


Nginx shall rewrite /assets/css/main.1448958665.css to /assets/css/main.css. But trying to get that file, it returns a 404.

This is my Nginx config for the site:

server {
    listen 80 default_server;

    server_name example.com;

    root /var/www/example.com;
    index index.php index.html index.htm;

    client_max_body_size 10M;

    rewrite ^/(content|site|kirby)$ /error last;
    rewrite ^/content/(.*).(txt|md|mdown)$ /error last;
    rewrite ^/(site|kirby)/(.*)$ /error last;

    location /assets {
        if (!-e $request_filename) {
            rewrite ^/(.+)\.(\d+)\.(js|css)$ /$1.$3 break;
        }
    }

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

    location /panel {
        try_files $uri $uri/ /panel/index.php?$uri&$args;
    }

    location ~ (?:^|/)\. {
        deny all;
    }

    location ~ (?:\.(?:bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$ {
        deny all;
    }

    location ~* \.(svg|js|css|png|jpg|jpeg|gif|ico|woff|woff2|ttf|eot)$ {
        expires 1y;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
    }
}

I am using Kirby CMS with the Cachebuster plugin if that does help.


Solution

  • The problem relates to how nginx processes a request. Your location ~* \.(svg|js|css|png|jpg|jpeg|gif|ico|woff|woff2|ttf|eot)$ takes precedence over location /assets.

    A simple fix (assuming that it contains no php) would be to move its precedence above regex by writing it as:

    location ^~ /assets { ... }
    

    In which case you may want to add an expires directive to the container also.