Nginx イメージファイル名は正規表現で URI を変更して寸法を削除します

Nginx イメージファイル名は正規表現で URI を変更して寸法を削除します

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 ;
}

関連情報