![Nginx イメージファイル名は正規表現で URI を変更して寸法を削除します](https://rvso.com/image/697381/Nginx%20%E3%82%A4%E3%83%A1%E3%83%BC%E3%82%B8%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%90%8D%E3%81%AF%E6%AD%A3%E8%A6%8F%E8%A1%A8%E7%8F%BE%E3%81%A7%20URI%20%E3%82%92%E5%A4%89%E6%9B%B4%E3%81%97%E3%81%A6%E5%AF%B8%E6%B3%95%E3%82%92%E5%89%8A%E9%99%A4%E3%81%97%E3%81%BE%E3%81%99.png)
nginx 設定で、WordPress の画像命名/サイズ表記規則に従って必要な画像サイズが見つからなかった場合に、元の画像を返す最適な方法は何ですか。
つまり、/image-name-150x170.png が見つからない場合は、/image-name.png を返すようにします。-150-170 の部分は他の数字にすることができます。したがって、ファイル名のドットの前の 1-4 桁のダッシュ x 1-4 桁を削除します。
@static_full ロケーション ブロック内の uri コードの置換または書き換えを行いたいのですが、パフォーマンス的にはどちらが優れているのか疑問に思っています。
#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;
}
答え1
try_files
ディレクティブの代わりにを使用することを検討してくださいerror_page
。
try_files $uri @static_full;
見るこのドキュメント詳細については。
編集 - 完全な解決策を追加しました:
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 ;
}