Apache2 [SOLVED]: Apache caching only works sometimes

Apache2 [SOLVED]: Apache caching only works sometimes

Home Forums Apache2 Apache2 [SOLVED]: Apache caching only works sometimes

Tagged: , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36891

    Anonymous

    QuestionQuestion

    I’m trying to set up caching on my Apache site starting with a basic configuration that I can test a PHP script with a browser:

    CacheEnable disk /
    CacheRoot /var/www/cache
    CacheDefaultExpire 3600
    CacheMinExpire 3600
    CacheIgnoreNoLastMod On
    CacheIgnoreCacheControl On
    

    However, it only works if the page is not “redirected” in some way. For example, I’m testing this simple PHP script in the root directory called “test.php”:

    <?php
        echo date('h:i:s');
    ?>
    

    If I go to “…/test.php” in my browser, it works: the output time doesn’t change after the first visit.

    If I go to “…/test”, it doesn’t work: the output always changes to the current time.

    Two notes:

    1. When it works, I see an “age” field in the cached response with an updated number of seconds since the page was first served. When it doesn’t work, there is no “age” field in the response.
    2. I don’t have an .htaccess file in the root directory, and I haven’t been able to find where Apache knows to serve the script without the “php” extension. When I test using an .htaccess file with a RewriteRule, it doesn’t cache the response when it matches. Not sure if this matters.

    Any ideas? Thanks!

    #36892

    Anonymous

    Accepted AnswerAnswer

    Typically you need to set your caching headers correctly for page caching to work. There’s a good article here, and Apache docs here.

    In summary of that article, you want to set the Cache-Control header. You can set etag, but the rest like pragma you shouldn’t bother with.

    Cache-Control: max-age=86400
    

    In Apache this is done with mod_expires, which would be configured something like this.

    # enable expirations
    ExpiresActive On
    # expire GIF images after a month in the client's cache
    ExpiresByType image/gif A2592000
    # HTML documents are good for a week from the
    # time they were changed
    ExpiresByType text/html M604800
    

    Source: https://serverfault.com/questions/890139/apache-caching-only-works-sometimes
    Author: Tim
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.