A
A
Alex2020-01-18 16:35:58
Nginx
Alex, 2020-01-18 16:35:58

How to make a NOIMAGE stub for an image folder in Nginx?

There is an image folder in the root of the site (/usr/share/nginx/html/image) with images.
Please tell me how to display a stub with a noimage image (/usr/share/nginx/html/noimage.png) if there is a request for an image from the image folder that does not exist, and do this only if the request is for the image folder. Those. if the request is site.ru/image/145.jpg and there is no file, then instead of 404 or another error, the image /usr/share/nginx/html/noimage.png is displayed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2020-01-18
@deepblack

It is possible like this:

location ~ \.(png|jpg)$ {
    error_page 404 /404.png;
}

Or like this:
location /images/ {
    try_files $uri /images/default.jpg;
}

location = /images/default.jpg {
    expires 30s;
}

nginx.org/ru/docs/http/ngx_http_core_module.html#t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question