Can't get cache controle to set an expire date

Hi,

This is my nginx config file: http://pastebin.com/TZcfzipk

The problem is that despite setting “expires max” cache sends no
expire date so I must be doing something wrong.
This is what appears as header:

Cache-Control max-age=0

Any idea how I could fix this?

Thanks in advance,

Pat

location /images/ {

# serve from disk and set expires

expires max;

}

location /stylesheets/ {

expires max;

}

location /javascripts/ {

expires max;

}

Solution:

Try this instead of giving the folders explicitly.,

location ~* .(ico|css|js|gif|jpe?g|png)(?[0-9]+)?$ {
expires max;
break;
}

TML

Hello!

On Fri, Sep 16, 2011 at 01:32:10AM -0500, Patrick A. wrote:

Any idea how I could fix this?
Please show request and response. (And please make sure you
actually see request in nginx logs, i.e. don’t test your browser’s
cache.)

If request actually hits nginx in question, I see two most likely
reasons:

  1. Your request is processed in location where you don’t have
    “expires max;” set. I.e. request doesn’t match /images/ etc.

  2. Your request is processed in another server block.

Maxim D.

Hello!

On Fri, Sep 16, 2011 at 11:49:34AM +0200, Mahalingam Mr wrote:

expires max;

}

Solution:

Try this instead of giving the folders explicitly.,

No, this is bad idea. Using regexp locations is bad as it makes
configuration support much harder. Using separate prefixes for
static files is much more scalable aproach.

Additionally, this particular snippet is incorrect and misleading
in serveral ways.

location ~* .(ico|css|js|gif|jpe?g|png)(?[0-9]+)?$ {

Testing “?..” is meaningless: query string isn’t tested by
“location” directives.

   expires max;
   break;

Directive “break” here does nothing but wastes cpu cycles.

}

Maxim D.

Hello!

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

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 D.

2011/9/16 Maxim D. [email protected]:

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;
}

Thanks! That worked great.

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?

Thanks,

Pat