# Can't get cache controle to set an expire date

**URL:** <https://www.ruby-forum.com/t/cant-get-cache-controle-to-set-an-expire-date/211890>\
**Category:** NGINX\
**Created:** [September 16, 2011, 8:32am UTC](https://www.ruby-forum.com/t/cant-get-cache-controle-to-set-an-expire-date/211890 "2011-09-16T08:32:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [September 16, 2011, 8:32am UTC](https://www.ruby-forum.com/t/cant-get-cache-controle-to-set-an-expire-date/211890/1 "2011-09-16T08:32:48Z")

</div>

Hi,

This is my nginx config file: [nginx conf - Pastebin.com](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

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [September 16, 2011, 11:49am UTC](https://www.ruby-forum.com/t/cant-get-cache-controle-to-set-an-expire-date/211890/2 "2011-09-16T11:49:34Z")

</div>

# 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

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [September 16, 2011, 12:14pm UTC](https://www.ruby-forum.com/t/cant-get-cache-controle-to-set-an-expire-date/211890/3 "2011-09-16T12:14:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [September 16, 2011, 12:20pm UTC](https://www.ruby-forum.com/t/cant-get-cache-controle-to-set-an-expire-date/211890/4 "2011-09-16T12:20:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [September 17, 2011, 12:52am UTC](https://www.ruby-forum.com/t/cant-get-cache-controle-to-set-an-expire-date/211890/5 "2011-09-17T00:52:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [September 17, 2011, 2:09am UTC](https://www.ruby-forum.com/t/cant-get-cache-controle-to-set-an-expire-date/211890/6 "2011-09-17T02:09:57Z")

</div>

2011/9/16 Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [September 16, 2011, 10:29pm UTC](https://www.ruby-forum.com/t/cant-get-cache-controle-to-set-an-expire-date/211890/7 "2011-09-16T22:29:55Z")

</div>

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
