# Expires header for .css

**URL:** <https://www.ruby-forum.com/t/expires-header-for-css/136864>\
**Category:** Mongrel\
**Created:** [May 6, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/expires-header-for-css/136864 "2008-05-06T22:55:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottd72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@scottd72](https://www.ruby-forum.com/u/scottd72)\
**Post date:** [May 6, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/expires-header-for-css/136864/1 "2008-05-06T22:55:33Z")

</div>

I’m using mongrel as my http server.

I was using yslow to evaluate the performance of my web site and noticed  
I was downloading the .css file on every request??? I use one .css file  
for the whole site and I though I would be cached?

Yslow indicates that there is no expires date/time set for the css file.

How can I tell mongrel to send an expires header with my static content?

thanks,  
Scott

---

<div class="post-metadata">

**Author:** ![scottd72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@scottd72](https://www.ruby-forum.com/u/scottd72)\
**Post date:** [May 6, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/expires-header-for-css/136864/2 "2008-05-06T23:14:46Z")

</div>

On Tue, May 6, 2008 at 1:55 PM, Scott D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How can I tell mongrel to send an expires header with my static content?

Seems Ben Schwarz ran into the same:

[http://germanforblack.com/2007/extreme-expires-headers-for-nginx-and-mongrel](http://germanforblack.com/2007/extreme-expires-headers-for-nginx-and-mongrel)

Would this come into play?

$ tail -3 config/environments/production.rb

# Don’t want asset-ids

# Want browser caching

ENV[‘RAILS\_ASSET\_ID’] = ‘’ # blank.

## Stephan

Stephan W.

→ [http://stephan.sugarmotor.org](http://stephan.sugarmotor.org)  
 → [http://www.thrackle.org](http://www.thrackle.org)  
 → [http://www.buckmaster.ca](http://www.buckmaster.ca)  
 → [http://www.trafficlife.com](http://www.trafficlife.com)  
 → [http://stephansmap.org](http://stephansmap.org)

---

<div class="post-metadata">

**Author:** ![scottd72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@scottd72](https://www.ruby-forum.com/u/scottd72)\
**Post date:** [May 6, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/expires-header-for-css/136864/3 "2008-05-06T23:19:28Z")

</div>

Is your webserver directly talking to users or are you using a proxy?

You can do this in mongrel, but generally you can configure your proxy  
to  
serve these files directly. It’s much faster to serve them statically.

For nginx, you can use the following…  
[http://wiki.codemongers.com/NginxRubyonRailsMongrel](http://wiki.codemongers.com/NginxRubyonRailsMongrel)

in your server block, add the following (it will statically serve and  
set  
the expiration date to the maximum allowed for images, javascript and  
css).

location ~\* .(js|css|jpg|jpeg|gif|png)$ {  
if (-f $request\_filename) {  
expires max;  
break;  
}  
}

Note that you’ll have to make sure that when you change  
css/javascript/images to either create a new file or add query  
parameters to  
defeat the caching (this is done for you in rails by default).  
Additionally, if you’re dynamically rendering CSS or JS, then this will  
break as well.

Also make sure that you’ve configured gzip compression for text files  
(css/javascript) in nginx as well. This will also give you a big speed  
improvement for most of your clients.

- scott

---

<div class="post-metadata">

**Author:** ![scottd72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@scottd72](https://www.ruby-forum.com/u/scottd72)\
**Post date:** [May 6, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/expires-header-for-css/136864/4 "2008-05-06T23:50:51Z")

</div>

Scott W. wrote:

> Is your webserver directly talking to users or are you using a proxy?

At this point mongrel is talking directly to the users. I’ve been  
looking at nginx but since I’m in a critical phase of development right  
now I didn’t want to enter any new services at this time.

> You can do this in mongrel, but generally you can configure your proxy

Yes I would like to do this in mongrel. Pre recomendation I added this  
to my environmnet.rb

if defined? Mongrel::DirHandler  
module Mongrel  
class DirHandler  
def send\_file\_with\_expires(req\_path, request, response,  
header\_only=false)  
response.header[‘Cache-Control’] = ‘max-age=315360000’  
response.header[‘Expires’] = (Time.now + 10.years).rfc2822  
send\_file\_without\_expires(req\_path, request, response,  
header\_only)  
end  
alias\_method :send\_file\_without\_expires, :send\_file  
alias\_method :send\_file, :send\_file\_with\_expires  
end  
end  
end  
but it doesn’t add the expires header to the css file.

thanks for your help in this matter,

Scott
