K
K
Konstantin Eliseev2019-05-22 21:40:14
Nginx
Konstantin Eliseev, 2019-05-22 21:40:14

How to enable image caching in nginx?

A request to a png image returns ETAG, but it should be Cache-Control. Can someone tell me how to get the files to be cached? Website: Laravel, nginx, php-fpm

# Response headers
date: Wed, 22 May 2019 18:31:50 GMT
etag: "5ce520ac-17d1"
last-modified: Wed, 22 May 2019 10:13:00 GMT
server: nginx/1.14.0 (Ubuntu)
status: 304
strict-transport-security: max-age=15768000; includeSubdomains; preload
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN

server {
        charset utf-8;
        client_max_body_size 128M;
        listen 443 ssl default_server http2;
        server_name SITE;

        ssl on;
        ssl_certificate KEY;
        ssl_certificate_key KEY;
        ssl_trusted_certificate KEY;
        include snippets/ssl.conf;
        include snippets/letsencrypt.conf;

        gzip on;
        gzip_comp_level 5;
        gzip_disable "msie6";
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        root /home/SITE/public;

        index index.php index.html;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            #...
        }

        location ~* \.(?:bmp|gif|jpeg|jpg|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|woff|woff2|txt|tif|doc|pdf|rtf|ttf|xls|ppt|rar|swf|zip)\$ {
                expires 30d;
                access_log off;
                add_header Pragma public;
                add_header Cache-Control "public";
        }

        location ~* \.(?:css|js)\$ {
                expires 14d;
                access_log off;
        }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim, 2019-05-22
@K0r5hun

Add the following line to the end of the config:
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
Do you have an extra space there?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question