Score:1

Nginx static files caching from different folders

cn flag

The problem is nginx does not display images and shows 404 not found on some folders. When i remove caching from config everything works fine. Trying to configure nginx to cache static files with this config

location ~* \.(?:css|cur|js|jpg|jpeg|webp|gif|htc|ico|png|html|xml|otf|ttf|eot|woff|woff2|svg)$ {

                expires 1y;
                access_log off;
                add_header Cache-Control "public";
                tcp_nodelay off;
                open_file_cache max=3000 inactive=120s;
                open_file_cache_valid 45s;
                open_file_cache_min_uses 2;
                open_file_cache_errors off;
}
        # pass PHP scripts to FastCGI server
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                include fastcgi_params;
                fastcgi_intercept_errors on;
        }
  location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

here is error log

    2021/08/17 11:08:10 [error] 278986#278986: *3642 open() "/var/www/website/public/cache/medium/product/347/rC0dMIdOJIJNSmpKgm9pVqKVE59HKAl8SKujwxHF.jpg" failed (2: No such file or directory), client: 95.85.108.178, server: ozan.com.tm, request: "GET /cache/medium/produ
ct/347/rC0dMIdOJIJNSmpKgm9pVqKVE59HKAl8SKujwxHF.jpg HTTP/2.0", host: "www.website.tm", referrer: "https://www.website.tm/"

nginx displays images from source: https://website.tm/storage/velocity/category_icon_path/77/5wiasmLf6hQGAsjsTV4jXsjnG0ELm5ak0rgpV7c2.png

nginx does not display from: https://website.tm/cache/medium/product/353/jtTzvdT8ZmB6Lu7wFKj969Uzj0qqu1qRUt2CxEbz.jpg

CodingInTheUK avatar
cn flag
What is the purpose of caching images? As you said, they are static content. I may be wrong here and If I am I hope somebody comes in to correct me but I believe you only need to cache "dynamic" content. Say your webpages are coming from a database, you would cache pages that change infrequently to prevent regenerating the page every load in doing so speeding up the loading. The images to my mind are the same the origin or the cache, unless you are generating smaller images on the fly. Then yes cache those.
CodingInTheUK avatar
cn flag
You may find this useful: https://serverfault.com/questions/861565/nginx-cache-images-generated-by-backend
Michael Hampton avatar
cz flag
Indeed, there is very little point to caching static assets again as they are already cached for you in memory by the operating system, and nginx is just writing a second copy on disk which it then has to spend extra time looking for.
Score:1
us flag

Your image location block is missing try_files directive, which tells what nginx should serve for requests hitting that location.

Add

try_files $uri $uri/ =404;

to the location block.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.