E
E
Evgeny Gorbov2021-04-12 16:14:41
Nginx
Evgeny Gorbov, 2021-04-12 16:14:41

Nginx location - auto-substitute another image in the absence of the requested image?

There is a set of pictures located on the server at:

/var/www/site.com/web/images/storage/icons/ИКАНОКА.png


It is simply requested from the browser:
https://site.com/images/storage/icons/TEST1.png

You need to check for the presence of icons by the file name:
1. First, the requested “TEST1.png”:
https://site.com/images/storage/icons/TEST1.png
if it exists → give it out

2. Then it’s the same but “.gif”
https://site.com/images/storage/icons/TEST1.gif
if it exists → give it

out 3. If there is no p. 1, no item 2 - issue a stub:
/images/storage/icons/_NULL.png

This is not how it works:
location ^~ /images/storage/icons/([a-zA-Z0-9]{1,9})\.(gif|png)$ {
    try_files $uri /images/storage/icons/$1.png /images/storage/icons/$1.gif /images/storage/icons/_NULL.gif =404;
}


so too:
location ^~ /images/storage/icons/([a-zA-Z0-9]{1,9})\.(gif|png)$ {
    try_files $uri /var/www/site.com/web/images/storage/icons/$1.png /var/www/site.com/web/images/storage/icons/$1.gif /var/www/site.com/web/images/storage/icons/_NULL.gif =404;
}


Tell me, pliz, how to set up the check correctly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question