在nginxconfiguration中,如果给定wordpress图像命名/大小符号约定,找不到所需的图像大小时,返回原始图像的最佳方法是什么。
所以,如果找不到/image-name-150×170.png,我想要返回/image-name.png。 -150-170部分可以是其他一些数字。 因此,我想在文件名中的点前1-4位数字x 1-4位删除。
我想把replace在uri代码里面@static_full位置块或重写。 想知道哪个更好的performance明智。
#some locations here and then location ~* ^.+\.(png|gif|jpg|jpeg){ access_log off; log_not_found off; expires max; error_page 404 = @static_full; #if not found, seek #static_ful } location @static_full{ #modify uri here to remove image dimensions like below #uri = remove dash 1-4 digits x 1-4 digits before dot #or rewrite to original name } location / { try_files $uri $uri/ /index.php?$args ; }
更新,我想出了如何做到这一点。 下面做了我想做的事情。
location @static_full{ #modify uri here to remove image dimensions like below #uri = remove dash three digits x three digits before dot rewrite "^(.*)(-[\d]{1,4}+x[\d]{1,4}+.)([\w]{3,4})" $1.$3 break; }
你可能会考虑使用try_files
而不是error_page
指令。
try_files $uri @static_full;
详情请参阅此文件 。
编辑 – 增加了完整的解决scheme:
location ~* ^.+\.(png|gif|jpg|jpeg) { try_files $uri @static_full; access_log off; log_not_found off; expires max; } location @static_full { rewrite "^(.*)(-[\d]{1,4}+x[\d]{1,4}+.)([\w]{3,4})" $1.$3 break; } location / { try_files $uri $uri/ /index.php?$args ; }