Trying to implement “far-future expiration date” for static files in Centos 5. Doesn't WANT to work. Help!?

Trying to implement “far-future expiration date” for static files in Centos 5. Doesn't WANT to work. Help!?

by Frankie Kam -
Number of replies: 5
Picture of Plugin developers

Hi y'all.

This one is driving me nuts.

When I run my site http://scm.moodleace.com through this great site analyzer on: http://gtmetrix.com/

I get a score of 80% for YSlow and 82% for Google Speed Grades.
However, I get an 'F' score for the  "Add Expires headers" section
of the analysis result page of gtmetrix.com for my site. Below is a snippet of the detailed report:

There are 12 static components without a far-future expiration date.
scm.moodleace.com/theme/anomaly_orange/styles.php
scm.moodleace.com/lib/javascript-mod.php
scm.moodleace.com/blocks/graph_stats/graph.php?course_id=1
scm.moodleace.com/file.php/1/icons/02f-eng.png
scm.moodleace.com/file.php/1/icons/01f-it.png
scm.moodleace.com/file.php/1/icons/03f-biz.png
scm.moodleace.com/file.php/1/icons/04f-hos.png
scm.moodleace.com/file.php/1/icons/08f-free.png
scm.moodleace.com/file.php/1/icons/09f-gam.png
scm.moodleace.com/user/pix.php/6/f2.jpg
scm.moodleace.com/file.php/1/icons/moodle20_f.png
scm.moodleace.com/file.php/1/icons/apture_effect_f.png

OKAY, so I set out to add far future expiration dates to these file types,
and so far I have failed spectacularly. Miserably. Desperately. black eye

What did I do?

First, I checked that mod_expires was actually already installed on my Centos.
A quick linux command of httpd -M of revealed that it was installed.

So I merrily edited my /etc/httpd/conf/httpd.conf file by adding in this code:

# CONFIGURE media caching
#
Header unset ETag
FileETag None

LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so

ExpiresActive On
<FilesMatch "\.(ico|pdf|flv|jpe?g|png|gif|js|css|swf)$">
ExpiresDefault "access plus 1 year"
</FilesMatch>

After that, I restarted Apache with this code:
/etc/init.d/httpd restart

Lastly, I ran the site through this great site analyzer on Firefox:
http://gtmetrix.com/

AND I STILL GET AN 'F' GRADE FOR "Add Expires Headers". See attached jpg.

A few more details:

My Php version (running on Moodle 1.9.7) is 5.2.17. I don't have any .htaccess file in my root (/) directory. Oh yeah, I'm also
running eAccelerator on my site, mod_deflate and munin reports.

If I can get the far -future expiration date for my static files to work,
that would be almost perfect. I would be thrilled, ecstatic and over the moon.
This would bump my YSlow and Page Speed Grades up a couple of notches. tongueout

Can any kind soul on Moodle.org advise me - what am I not doing right??? mixed

Frankie Kam
Melaka, Malaysia

Average of ratings: -
In reply to Frankie Kam

Re: Trying to implement “far-future expiration date” for static files in Centos 5. Doesn't WANT to work. Help!?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You don't acutally want to do that.

Most of the thinks that in that list of "12 static components without a far-future expiration date" are not really static.

So, if you add a far-future expiration date, and then want to change one of those items (for example a new version of the CSS or one of the images) then you are stuck. Even if you upload the new version, most users won't see it, becuase they have the old version cached.

So, the problem here is why the relatively dumb performance-check script, which does not undersand what is really going on. Doing what it says would be a mistake.

 

The right solution, which Moodle 2.0 adopts for CSS and JS, can be seen by doing viw source on this web site. Note that the main link to bring in CSS links to a single file:

http://moodle.org/theme/styles.php?theme=moodleofficial&rev=241

(there is only one other stylesheet link for YUI). The CSS is sent with an expires header

Expires: Thu, 10 Feb 2011 11:32:21 GMT

(Acutally I am surprised that is not further in the future.)

There several things to note about that:

  • That contains all the CSS from all the different plugins in Moodle (minimising HTTP requests)
  • It has been minified, that is, all the extra whitespace has been stripped out.
  • most importantly, the rev=241 at the end of the URL. Whenever any of the CSS changes, Moodle can increase this number, which ensures all users get the new version of the file, and stop using the old cached version.

Also note that if you look at the page head with Firebug, you will see a lot more complicated junk in the header. That is becuase most of the JavaScript is loaded only after the main content of the page is done so that it does not delay making the main page content available, and the way that is done is by addmin tags to head later.

In reply to Tim Hunt

Re: Trying to implement “far-future expiration date” for static files in Centos 5. Doesn't WANT to work. Help!?

by Frankie Kam -
Picture of Plugin developers

Tim

Thanks for the feedback and the detailed explanation of Moodle 2.0's handling of CSS. My head's spinning....thanks for the advice, so let's see...how do I proceed..?

In reply to Frankie Kam

Re: Trying to implement “far-future expiration date” for static files in Centos 5. Doesn't WANT to work. Help!?

by Greg Lund-Chaix -

How to proceed?  That's easy: ignore that part of the script.  It's not giving you a valid suggestion in this case.  The content the script thinks is static is, in fact, not static.  So caching it would cause problems for your users, presenting them with outdated, inconsistent, or nonexistnet content.

As Tim said, Moodle 2.0 improves how static content like CSS, javascript and images are handled with regard to caching.  So upgrading to 2.0 will definitely help in this case - both in real performance terms (it really does make the stuff more cache-able) and the test scripts will be happier because they're seeing what they want to see.

-Greg

In reply to Greg Lund-Chaix

Re: Trying to implement “far-future expiration date” for static files in Centos 5. Doesn't WANT to work. Help!?

by Frankie Kam -
Picture of Plugin developers

Greg

Thanks for the advice. Got it. You guys have saved me hours of going-down-another-rabbit-hole.

I've already installed Moodle 2.0 on my domain. http://m2.moodleace.com and am waiting for the time when I can restore 1.9.x courses onto it.  tongueout

Frankie

In reply to Tim Hunt

Re: Trying to implement “far-future expiration date” for static files in Centos 5. Doesn't WANT to work. Help!?

by Frankie Kam -
Picture of Plugin developers

Tim,

So I should take the resultand Grade readings with a pinch of salt(?).

I understand what you say about Moodle 2.0's handling of CSS and its way of handling cache versions. My production site is Moodle version 1.9.7., and to upgrade now is not an option yet.

Here's the funny thing, when I setup new subdomains and install Moodle 1.9.7 onto them, the performance checkup script signals that “far-future expiration date” is set up and running fine on those new subdomains. E.g., http://m.moodleace.com/ and http://test.moodleace.com/moodle/

Now I'm really stumped.