我有一个脚本,每次部署我的网站都会增加一个全局修订号。然后将该数字映射到加载 CSS、JavaScript 和 sprite 资产的 HTML。这被用作缓存清除策略。
例如<link rel="stylesheet" href="/css/screen_r123.css" type="text/css" />
在 Apache 中,我会将这些递增的 URL 重写为实际资产,如下所示:
RewriteRule ^css/screen_r(.*).css$ /css/screen_min.css [L]
我将如何在 nginx 中做同样的事情?我不确定在哪里放置正则表达式匹配逻辑。
注意:我不想将查询附加?r=123
到 URI 的末尾,因为将查询传递给静态资产感觉不正确,而且我在缓存哈希中不包含查询的 Varnish 代理后面
这是我当前网站的 nginx 配置:
server {
listen 8080;
server_name domain.com www.domain.com
port_in_redirect off;
root /usr/share/nginx/mydomain.com/public;
index index.html index.php;
#set long expiry for assets
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
#FOLLOWING LINE DOES NOT WORK AS INTENDED
rewrite ^/css/screen_r(.*).css$ /css/screen.css last;
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param APPLICATION_ENV "production";
fastcgi_split_path_info ^(.+.php)(.*)$;
}
add_header "X-UA-Compatible" "IE=Edge,chrome=1";
add_header Cache-Control "public max-age=60";
}