Setting 'expires' headers for files

61 views
Skip to first unread message

voltron

unread,
Jun 13, 2008, 6:16:43 AM6/13/08
to web2py Web Framework
'Yslow' from Yahoo informs me about seting 'expires' headers for my
files( CSS, images e.t.c) How can I achieve this in web2py?

Thanks

mdipierro

unread,
Jun 13, 2008, 10:15:07 AM6/13/08
to web2py Web Framework
response.headers['ache-Control']='no-cache'

for example. I remember we discuss making this automatic. I am not
sure we did or not. I do not have access to my computer quite yet. I
will check in one hour and get back with more on this...

Massimo Di Pierro

unread,
Jun 13, 2008, 11:15:04 AM6/13/08
to web...@googlegroups.com
right now we only fix pragma: no-cache. Isn't this sufficient? What's
Yslow?

voltron

unread,
Jun 13, 2008, 12:52:07 PM6/13/08
to web2py Web Framework
Yslow helps when you want to speed up your site:

http://developer.yahoo.com/yslow/

voltron

unread,
Jun 15, 2008, 2:51:42 PM6/15/08
to web2py Web Framework
I am looking for something similar to this exaple in PHP:

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");
$json = "";
$json .= "{\n";
$json .= "page: $page,\n";
$json .= "total: $total,\n";
$json .= "rows: [";
$rc = false;
while ($row = mysql_fetch_array($result)) {
if ($rc) $json .= ",";
$json .= "\n{";
$json .= "id:'".$row['iso']."',";
$json .= "cell:['".$row['iso']."'";
$json .= ",'".addslashes($row['name'])."'";
$json .= ",'".addslashes($row['printable_name'])."'";
$json .= ",'".addslashes($row['iso3'])."'";
$json .= ",'".addslashes($row['numcode'])."']";
$json .= "}";
$rc = true;
}
$json .= "]\n";
$json .= "}";
echo $json;


Thanks

Massimo Di Pierro

unread,
Jun 16, 2008, 12:39:56 AM6/16/08
to web...@googlegroups.com
response.headers["Expires"]=time.strftime("%W, %b %m %Y %H:%M:%S" ,time.time()+3600)+" GMT" #expires in 1h 
response.headers["Last-Modified " ]=time.strftime("%W, %b %m %Y %H:%M:%S" )+" GMT" 
response.headers("Cache-Control"]="no-cache, must-revalidate"
response.headers["Pragma]="no-cache" 
response.headers["Content-type]="text/x-json"

voltron

unread,
Jun 16, 2008, 2:34:44 AM6/16/08
to web2py Web Framework
So the next question, which I have asked before, how can this be
hacked to work for individual files in the "static" folder? :-)


Thanks

Massimo Di Pierro

unread,
Jun 16, 2008, 10:57:09 AM6/16/08
to web...@googlegroups.com
You can use os.stat to get the time of the files in the static folder
but mind that this is already done for you by web2py.

When you serve static files (from the static folder) web2py already
follows the IF_MODIFIED_SINCE protocol.

You only need to do something if you and streaming files yourself (in
which case the files should not be in static).

voltron

unread,
Jun 16, 2008, 11:05:17 AM6/16/08
to web2py Web Framework
Thanks for the reply. I understand, I wonder why Yslow states that my
test site has the lowest score for those static files if it it done
automatically by web2py. I will use a HTTP sniffer to see what headers
are being sent by web2py. The images that Yslow complains about are
JS, CSS and image files

Massimo Di Pierro

unread,
Jun 16, 2008, 11:31:12 AM6/16/08
to web...@googlegroups.com
If you are using apache, you can bypass web2py completely ad this may
speed up quite a bit.

voltron

unread,
Jun 16, 2008, 12:36:16 PM6/16/08
to web2py Web Framework
I would research a little on this and give you a feedback. Thanks!

Kyle Smith

unread,
Jun 16, 2008, 3:32:44 PM6/16/08
to web...@googlegroups.com
Here's an example of what you can put in your apache configuration to expire certain files by content type.

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/html "access plus 2 months"
  ExpiresByType image/gif "access plus 2 months"
  ExpiresByType image/jpeg "access plus 2 months"
  ExpiresByType image/png "access plus 2 months"
  ExpiresByType text/css "access plus 2 months"
  ExpiresByType text/javascript "access plus 2 months"
  ExpiresByType application/x-javascript "access plus 2 months"
</IfModule>

Kyle

Kyle Smith

unread,
Jun 16, 2008, 3:33:37 PM6/16/08
to web...@googlegroups.com
Here's an example of what you can put in your apache configuration to expire certain files by content type.

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/html "access plus 2 months"
  ExpiresByType image/gif "access plus 2 months"
  ExpiresByType image/jpeg "access plus 2 months"
  ExpiresByType image/png "access plus 2 months"
  ExpiresByType text/css "access plus 2 months"
  ExpiresByType text/javascript "access plus 2 months"
  ExpiresByType application/x-javascript "access plus 2 months"
</IfModule>

Kyle

On Mon, Jun 16, 2008 at 9:36 AM, voltron <nhy...@googlemail.com> wrote:

Massimo Di Pierro

unread,
Jun 16, 2008, 3:39:07 PM6/16/08
to web...@googlegroups.com
Mind that this will be ignored if you go over the proxy for the static files. You sill need to bypass the proxy.

Kyle Smith

unread,
Jun 16, 2008, 4:09:03 PM6/16/08
to web...@googlegroups.com
That's very true, the static files would need to be served by apache. I believe this setup would work easily for those using mod_wsgi, or fcgi method rather than proxy. Of course assuming that all static files are, well static, we could then have apache configured to proxy all paths except /appname/static and set Apache to serve all files in that path which would also apply the settings in mod_expires.

Kyle

voltron

unread,
Jun 17, 2008, 3:06:41 AM6/17/08
to web2py Web Framework
Hmm, this is going to be more complicated as I expected. What about
creating a way to set these headers for files in the static folder?
This folder is non-configurable anyway?

On Jun 16, 10:09 pm, "Kyle Smith" <kyletsm...@gmail.com> wrote:
> That's very true, the static files would need to be served by apache. I
> believe this setup would work easily for those using mod_wsgi, or fcgi
> method rather than proxy. Of course assuming that all static files are, well
> static, we could then have apache configured to proxy all paths except
> /appname/static and set Apache to serve all files in that path which would
> also apply the settings in mod_expires.
>
> Kyle
>
> On Mon, Jun 16, 2008 at 12:39 PM, Massimo Di Pierro <mdipie...@cs.depaul.edu>

voltron

unread,
Jun 18, 2008, 10:52:51 AM6/18/08
to web2py Web Framework
This is still somewhat open Massimo. This is another version of what I
am after:

# PHP code

$HTTP["url"] =~ "\.(jpg|gif|png|js|css)$" {
expire.url = ( "" => "access 1 months" )
}

This sets the expires headers of different types of files
collectively. There should be something like this for web2.py.

Thanks

Massimo Di Pierro

unread,
Jun 18, 2008, 10:57:01 AM6/18/08
to web...@googlegroups.com
This would invalidate the IF_MODIFIED_SINCE protocol which web2py
supports and is better.
Can this be done in apache?

Massimo

voltron

unread,
Jun 18, 2008, 11:08:36 AM6/18/08
to web2py Web Framework
Ok, Ill use Kyles example above for Apache. Thanks!
Reply all
Reply to author
Forward
0 new messages