1

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。

4

1 回答 1

1

问题与nginx 如何处理请求有关。Yourlocation ~* \.(svg|js|css|png|jpg|jpeg|gif|ico|woff|woff2|ttf|eot)$优先于location /assets.

一个简单的修复(假设它不包含 php)是将其优先级移动到正则表达式之上,将其编写为:

location ^~ /assets { ... }

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

于 2015-12-02T16:11:05.273 回答