can't get cache controle to set an expire date

Maxim Dounin mdounin at mdounin.ru
Fri Sep 16 22:51:42 UTC 2011


Hello!

On Fri, Sep 16, 2011 at 03:29:23PM -0500, Patrick Aljord wrote:

> Hey Maxim,
> 
> Ok so after investigation I realized that this one worked:
> 
> /images/q-icon-medium.png
> 
> But these two did not have expire cache:
> 
> /packages/base.js
> /_files/groups/medium/4af798a119ce955bd1000001/0.png
> 
> So I added this to the conf:
> 
> 
>    location /packages/ {       expires max;   }
>    location /_files/ {
>        expires max;
>    }
> 
> This fixed the '/packages/base.js' cache but I started getting a 404
> on the '/_files/groups/medium/4af798a119ce955bd1000001/0.png'. So I
> removed the "location /_files/" from the conf. The thing is that this
> file does not come from the hard drive. It comes from the web app (I'm
> using unicorn for rails + nginx). Any idea how I should set the expire
> headers for those assets that come from the app? Should I set them in
> the rails app?

Either set it from app, or pass to app while still setting 
expires.  Something like this should work:

    location /_files/ {
        expires max;
        proxy_pass http://foo_app_server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
    }

If some data in /_files/ may be actually static files on disk, you 
may want to add try_files with named location, much like in 
"location /" + "location @app" in your config, but with expires 
set.  I.e. something like this:

    location /_files/ {
        expires max;
        try_files ... @appstatic;
    }

    location @appstatic {
        expires max;
        proxy_pass http://foo_app_server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
    }

Maxim Dounin



More information about the nginx mailing list