0

I am trying to configure Apache browser caching, but it doesn't seem to be working half the time.

This is my vhost config:

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerName example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/example

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <Directory /var/www/example>
                <IfModule mod_expires.c>
                    <FilesMatch ".+\.(jpe?g|png|gif|js|css|woff|woff2|svg|eot|ttf)$">
                        ExpiresActive On
                        ExpiresDefault "access plus 2 weeks"
                    </FilesMatch>
                </IfModule>

                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all

        </Directory>

        RewriteEngine on

        SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
    </VirtualHost>
</IfModule>

Here's a curl test:

$ date -u
Fri 11 Aug 2017 00:00:47 UTC

$ curl -s -v -o /dev/null  https://example.com/logo.png
*   Trying 34.227.118.252...
* TCP_NODELAY set
* Connected to example.com (34.227.118.252) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: example.com
* Server certificate: Let's Encrypt Authority X3
* Server certificate: DST Root CA X3
> GET /logo.png HTTP/1.1
> Host: example.com
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 11 Aug 2017 00:02:06 GMT
< Server: Apache/2.4.18 (Ubuntu)
< Content-Length: 2527
< Accept-Ranges: bytes
< X-Original-Content-Length: 2696
< Etag: W/"PSA-aj-_N7_D464wY"
< Expires: Fri, 11 Aug 2017 00:06:01 GMT         # <---- ??
< Cache-Control: max-age=234
< X-Content-Type-Options: nosniff
< Content-Type: image/png
<
{ [2527 bytes data]
* Curl_http_done: called premature == 0
* Connection #0 to host example.com left intact

The Expires tag is just a few minutes into the future, whereas it should be a 2 week expiry according to the vhost configuration.

Weirdly, testing in a browser, some images do get sent with the correct expiry, but others in the same server vhost don't. mod_pagespeed is enabled, but I don't think this should change things? Any ideas?

Apache/2.4.18 (Ubuntu)

Dagrada
  • 153
  • 1
  • 1
  • 6
  • Website in question is https://strongproject.com/ – Dagrada Aug 11 '17 at 01:35
  • 1
    what you have should work perfectly (I tested it). Try setting "allowoverride none" to see if something in .htaccess is "overriding" your config. Another option would be you are landing in the incorrect virtualhost. Sidenotes: Why use .htaccess when you have admin access. You are complicating your life. Also if this is really 2.4 you should use Require instead of Allow/Deny/Order. – Daniel Ferradal Aug 11 '17 at 12:23
  • I just realised it's mod_pagespeed that's causing the problem! – Dagrada Aug 11 '17 at 12:27
  • 1
    Found plenty of info on the Pagespeed caching problem, including https://modpagespeed.com/doc/downstream-caching. Ironic because Google's own Pagespeed Insights is complaining that I'm not caching the files for long enough. I guess this is a good reason to move all my static images to CDN and the problem will be mitigated. – Dagrada Aug 11 '17 at 12:30

0 Answers0