0

In nginx config, what is the best way to return the original image when the required image size was not found given wordpress image naming/size notation conventions.

So, say if /image-name-150x170.png not found, I want /image-name.png to be returned. -150-170 part can be some other numbers. Therefore, I want dash 1-4 digits x 1-4 digits before dot in the file name to be removed.

I want to put the replace in uri code inside @static_full location block or rewrite. wondering which would be better performance-wise.

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

UPDATE, I figured out how to do it. The following did what I wanted to do.

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; } 
2
  • Why not use a capture group inside your regular expression and a try_files in the location? Or do you actually want the return code to be a 404? Commented Jan 23, 2017 at 0:21
  • I found out how to do it. Please see the update above. It works great. Commented Jan 23, 2017 at 0:29

1 Answer 1

1

You might consider using try_files instead of the error_page directive.

try_files $uri @static_full; 

See this document for details.

EDIT - added full solution:

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 ; } 
5
  • Only one try_files can be used. If you use it in one of the location blocks, you can not use it again. I have to use try_files in another location because the site is not just static image files. You can check here: linuxplayer.org/2013/06/… stackoverflow.com/questions/21286850/… stackoverflow.com/questions/37962804/… Commented Jan 23, 2017 at 11:04
  • Wrong. You can have as many try_files statements as you want. The error_page hack is to cascade named location blocks because try_files can only have one default action. Commented Jan 23, 2017 at 11:31
  • But doesnt work. I only used that solution because try_files ddidnt work, it was common sense to try it and I thought it would work. When it didnt work, I tried searching. It seems using the last try_files directive even if you have multiple ones. Commented Jan 24, 2017 at 1:01
  • 1
    I don't understand the error you are observing with try_files. But I have edited the answer and added the solution I mean, which works for me. Of course, this has little to do with your question, which you answered yourself ;-) Commented Jan 24, 2017 at 8:03
  • I just tried and your solution works. Thank you. Is there any performance or any drawback of error_page 404 cascading instead of try_files? Commented Jan 25, 2017 at 5:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.