Nginx重写不起作用(Kirby CMS Cachebuster)

时间:2015-12-02 07:30:50

标签: nginx rewrite kirby

Nginx会将/assets/css/main.1448958665.css重写为/assets/css/main.css。但是尝试获取该文件,它会返回404

这是我网站的Nginx配置:

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

如果确实有帮助,我将使用带有Cachebuster插件的Kirby CMS。

1 个答案:

答案 0 :(得分:1)

问题与how nginx processes a request有关。您的location ~* \.(svg|js|css|png|jpg|jpeg|gif|ico|woff|woff2|ttf|eot)$优先于location /assets

一个简单的修复(假设它不包含php)将把它的优先级移到regex之上,将其写成:

location ^~ /assets { ... }

在这种情况下,您可能还想在容器中添加expires指令。