This question has been archived.

mod_expires Is active, but my server isn't caching images?

February 22, 2016 11.4k views
Caching Apache Ubuntu

I've been trying to reduce the load time for my website, and a big thing that Google says is wrong is that none of my stuff is Cached/ has an expiration date. I added code to set expires, enabled mod_expires, inspected element says it's working, and everything seems to be working just fine except that it isn't.

Why aren't my images being given an expiration and how can I fix this. I talked with DO support and they have no clue, so any help would be very much appreciated.

  • Since it's the client that honors the expiration of the client cached images, have you seen any difference in behaviour between different browsers?

  • gndo, As far as the images being assigned an expire, no. I've run the speed tests with emulated IE and Firefox and they both give me the same result. I don't think I can test it myself because I visit the site so often on my own computer, but I may be wrong.

  • @aldaszek - Instead of emulating a specific browser, how about using wget or curl with the response header being printed out to see if the expires and cache control headers are being returned for browser agnostic requests:

    wget -S

    Or is your site only active for supported browsers?

  • @gndo, it should be active for all browsers - nothing specific, at least not on purpose. I ran that command and got this:

    Connecting to (||:80... connected.
    HTTP request sent, awaiting response...
    HTTP/1.1 200 OK
    Date: Mon, 22 Feb 2016 04:22:53 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Last-Modified: Mon, 22 Feb 2016 01:23:09 GMT
    ETag: "4b98-52c51acfe1c10"
    Accept-Ranges: bytes
    Content-Length: 19352
    Vary: Accept-Encoding
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Content-Type: text/html
    Length: 19352 (19K) [text/html]
    Saving to: ‘index.html’

    100%[======================================>] 19,352 --.-K/s in 0s

    2016-02-21 23:22:53 (250 MB/s) - ‘index.html’ saved [19352/19352]

    Not sure if this response is good or bad...

  • @aldaszek - I don't see an "Expires:" header nor "Cache-Control:" in the response for your index.html file (assuming your index.html file has the images that are supposedly not being cached). I'm not sure why those headers are not being produced, unless restarting the server couldn't find the configuration changes you made (edit - or your directives were not correct). Sorry, that's all I have on this.

  • @gndo, I haven't done anything with Cache-Control. If it's a separate step besides just enabling the expires, then I haven't done anything with it so that could be why that isn't showing up. All I've really done is just add some code to the .htaccess file, "a2enmod expires" in my console, and that's about it. If there's a step I've missed, It's really just gone past me. Thank you for the help!

  • @aldaszek - Here's the manual for Apache 2.x mod_expires module and directives for your http.conf file. There are examples there that may be of use to you. Good luck.

  • @gndo, do I need to edit the http.conf file? I have not done anything with that.

    Here is what I have in the .htaccess file for my server, for those it may help help me. I had to hand type it so there may just be a typo in there:


    <IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresDefault "access plus 2 days"

    EXPIRES CACHING compress text, HTML, JavaScipt, CSS, and XML

    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript

    remove browser bugs

    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\0[678] no-gzip
    BrowserMatch /bMSIE !no-gzip !gzip-only-text/html
    Header append vary User-Agent

  • @aldaszek - Oops, I used a short hand for conf files for /etc/apache2 or your htaccess files. No, you don't have to create an httpd.conf file (I had a typo there too).

  • @aldaszek - You didn't mention if you did "service apache2 restart"

  • @gndo I did do a restart. Are there several types of restarts? Stupid question, but I'm not home right now to check. I recall doing /a/ restart, but not sure if it is a "service" restart - if that's a thing.

    http.conf, type or not, I have not done anything with that at all as of yet.

  • @aldaszek - Specifically, "service apache2 restart" on the command line after enabling mod_expires. BTW, I tried your "EXPIRES CACHING" section of your htaccess file and that worked in that it returned the "Expires:" and "Cache-Control:" response headers.

  • @gndo - I did do a restart before, but I did another one and checked and still no. I looked around a bit and in the console it gave this error:

    "The code on this page disabled back and forward caching. For more information, see:"

    I'll check out the link and see what I can find out.

  • @gndo - I didn't learn anything from the site, just a lot of terms I didn't know. I didn't know it was this complicated, wow!

Be the first one to answer this question.