While investigating a serious performance issue over the last 24 hours I discovered an issue whereby some of our CSS files were being combined but were not being rewritten by mod_pagespeed. After much hair pulling, far too many hours spent in front of
less and friends, I finally tracked down the solution. I added a
ModPagespeedLoadFromFile directive for every CDN domain and now all our resources are being properly rewritten.
I think, but I’m not sure, that mod_pagespeed tries to retrieve from the same domain as the request arrives on. So if you’re rewriting resources from
cdn.xmpl.tld, when the request arrives at mod_pagespeed with a Host header of
cdn.xmpl.tld, mod_pagespeed tries to look up every resource on the
cdn.xmpl.tld hostname, instead of doing a reverse lookup through the
Here’s the slides (pdf) from the presentation last night. I’ve pulled out a couple of the more useful code sections below. Any questions, or if you want any links not in the slides, let me know in the comments.
The first line rewrites static resources via a custom origin CDN, the second shards those resources between 2 or more CDN hostnames. We have a CloudFront distribution setup on cdn1|2.dmn.tld that uses domain.tld as the origin. No other config required.
ModPagespeedMapRewriteDomain static.dmn.tld domain.tld
ModPagespeedShardDomain static.dmn.tld cdn1.dmn.tld,cdn2.dmn.tld
We use these mod_pagespeed filters:
I’d recommend using the ModPagespeedLoadFromFile directive:
ModPagespeedLoadFromFile "http://static.dmn.tld/js/" "/var/www/path/to/htdocs/js/"
ModPagespeedLoadFromFile "http://static.dmn.tld/skin/" "/var/www/path/to/htdocs/skin/"
ModPagespeedLoadFromFile "http://static.dmn.tld/media/" "/var/www/path/to/htdocs/media/"
Few links for the truly lazy who don’t want to search!