Using Expire on Nginx config making all new images disappear

Events happening in the community are now at Drupal community events on www.drupal.org.
felixodie's picture

Hello there,

I've been trying to use browser cache more efficient so I thought using expire in Nginx would be a good idea.

However when I add this code to my Nginx config, all images I've uploaded after the config disappears.

To be more clear;

-Let's say I've uploaded file flower.jpg to my webpage. After upload it looks just fine.
-Then I go to nginx configuration and add following lines to it.
-After that I upload car.jpg file and it's not showing up anywhere. Not in my main page which I use a 100x200px image style, not in node where I use the original image.

Is there something wrong with the code? I'm trying to "Leverage browser cache" as google page speed insight tool recommends.

Thanks!

       location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
       root /home/site/public_html;
       expires 1y;
       add_header Pragma public;
       add_header Cache-Control "public, must-revalidate, proxy-revalidate";
       log_not_found off;

Comments

Do you really need to set a

Garrett Albright's picture

Do you really need to set a different web root for your images?

Are you sure you're

perusio's picture

setting the correct webroot?

Like Garret says above is quite fishy that you have a root directive inside your location. Also the location can be improved:

location ~* \.(?:js|css|png|jpe?g|gif|ico)$ {
       expires 1y;
       ## These here are not really necessary.
       #add_header Pragma public;
       #add_header Cache-Control "public, must-revalidate, proxy-revalidate";
       log_not_found off;
}

Hello there, Thank you for

felixodie's picture

Hello there,

Thank you for your replies.

I don't think I need to set a different web root, so i'll comment out that line.

Also, perusio, thank you. I'll try to use your config. However is there a typo at jpeg?

       location ~* .(?:js|css|png|jpe?g|gif|ico)$ {
       expires 1y;
       ## These here are not really necessary.
       #add_header Pragma public;
       #add_header Cache-Control "public, must-revalidate, proxy-revalidate";
       log_not_found off;
}

Hey guys,Just reporting that

felixodie's picture

Hey guys,

Just reporting that commenting out all other lines made it work.

These two lines did the trick :

location ~* .(?:js|css|png|jpe?g|gif|ico)$ {
expires 1y;

Thank you for your helps!

EDIT :

I've found out later that the config above is not working for image styles. Especially for the new ones. I've searched more and stumbled to this config :

        location ~* ^(?!/sites/default/files).*.(js|css|png|jpg|jpeg|gif|ico|woff)$ {
        expires 30d;
        try_files $uri @drupal;
        log_not_found off;
    }
 

You can check Klausi's blog for more information

Pragma deprecated

1kenthomas's picture

Note Pragma is essentially deprecated (it was never consistently adopted) and probably shouldn't be used. Cache-Control is fine. ;)

~kwt

Nginx

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: