Search code examples
cachingnginxbrowser-cache

how to fix Leverage browser caching nginx?


I went to the Internet, put all the possible settings for caching, but all the same the end of google page sped insights wrote that I do not have caching for these files how to fix this?

Shiw picture

my cache settings "nano /etc/nginx/nginx.conf"

 proxy_temp_path /var/nginx/proxy_temp;
 add_header X-Cache-Status $upstream_cache_status;
 proxy_cache_path /tmp/nginx/cache levels=1:2 keys_zone=one:100m;
 proxy_cache one;
 proxy_cache_valid any 30d;
 proxy_cache_key $scheme$proxy_host$request_uri$cookie_US;

my server conf "nano /etc/nginx/sites-enabled/theband"

location ~* ^(?!/media).*.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
    expires 365d;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    access_log off;
}
location ~* ^(?!/static).*.(?:css|js|html)$ {
    expires 365d;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    access_log off;
}

location / {
    proxy_set_header X-Real-IP $remote_addr;

    proxy_cache one;
    proxy_cache_min_uses 1;
    proxy_cache_use_stale error timeout;

Solution

  • "expires 35d;" this line need to remove, from where you defined it to create the storage of your files.

    from here:

       location ~* ^(?!/media).*.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
            root /tmp/nginx/trash/trash_media;
            expires 35d; # REMOVE THISSSSSSSS
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
            access_log off;
        }
    

    To here:

    server {
    
        listen   80;
        server_name myip;
        expires 35d;
        client_max_body_size 4G;
    

    and it will work, although in all absolutely examples, you do not need to put storage time there !!!!!!

    also don't forget to allow accept data from net and write and read it from local storage

    sudo chown www-data /tmp/nginx/proxy_temp
    sudo chmod 700 /tmp/nginx/proxy_temp
    sudo chown www-data /tmp/nginx/cache
    sudo chmod 700 /tmp/nginx/cache
    sudo chown www-data /tmp/nginx/cache2
    sudo chmod 700 /tmp/nginx/cache2