考虑到这样的地点:
location ~ ^/user-content/img/) {
root /srv/foo/bar/uploads/;
autoindex off;
access_log off;
expires 30d;
}是否可以使用nginx请求
/user-content/img/3AF1D3A69CE92ADAED8B0D25C2411595C7C798A5.png实际上是从目录/srv/foo/bar/uploads/3A/F1/D3提供服务,这将涉及从请求文件名中提取前两个字符并使用它们作为第一个子文件夹,然后对下一个更深的文件夹使用字符3-4,然后为最后一个子文件夹追加字符5-6?
发布于 2020-10-22 18:50:23
你可以尝试(没有测试)
location /user-content/img/ {
rewrite "^/user-content/img/(\w{2})(\w{2})(\w{2})(.*)" /$1/$2/$3/$1$2$3$4 break;
root /srv/foo/bar/uploads;
autoindex off;
access_log off;
expires 30d;
}<#>更新
给它做个测试。可以确认此方法也有效。正如OP所指出的,带大括号的regex应该在中引用。
发布于 2020-10-22 19:17:30
这一办法应奏效:
location ~ "^/user-content/img/([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})(.*)$" {
root /srv/foo/bar/uploads;
try_files /$1/$2/$3/$1$2$3$4 =404;
autoindex off;
access_log off;
expires 30d;
}在location行中,我们使用正则表达式将文件名的部分捕获到四个不同的变量,前三部分是目录,第四部分是文件名的其余部分。
在try_files指令中使用变量来创建图像名称的路径。
https://serverfault.com/questions/1039724
复制相似问题