Minification, Compression, Expiration

Updated 20-Sep-2023

A big part of speed is managing the browser cache. This usually means setting compression (gzip/deflate) and expirations of cached content. Compression takes place at the web server (generally after minification).

Minification usually also combines files together, so that that there are fewer requests. Compression provides enormous advantages (much more than minification), however, minification done in tandem with concatenation, and then cached (via Mod_Pagespeed + Memcached) is a very good tactic, previous to Gzipping at the Apache server (not to mention page, database, and object caching, as well as Opcache).

Yes, there are several moving parts here. It is important to test the various combinations, which takes a bit of time.

Google's Mod_Pagespeed

Google's Mod_Pagespeed tries to manage all three of these things, including image compression and changing image format types (jpg/png/gif to webp). There used to be significant difficulties in having Mod_Pagespeed work effectively in an SSL environment, though support is now built in by default via HttpsFetch. However, certificate directories still need to be set.

However, I've had a lot of trouble with pagespeed, so I don't use it (and am happier for it). Sometimes there is too much complexity, and too many different techniques trying to do too much. Best-of-breed is generally a better information technology bet than all-in-one.

Concatenation of Files

Mod_Pagespeed is not very good at concatenation of files, though it does a good job on image concatenation. In a WordPress environment, plugins usually help.

WordPress Minification and Caching Plugins

  • Autoptimize which performs excellent concatenation as well as expires.
  • Fix-W3TC which does page, database, and object caching, and can be configured to use Memcached.

Note that Autoptimize has an extensive API and is highly configurable. The developers provide an extensive walk-through of the Autoptimize algorithm.

Even with Autoptimize, Fix-W3tc, and Mod_Pagespeed, there are still two areas to implement, the Web server-based compression (Gzip/Deflate) and a Content-Delivery Network (CDN).

Configure Compression on Apache

Gzip compression on Apache uses the mod_deflate, mod_headers, and mod_gzip Apache modules. Also, Gzip is usually a supported Mimetype by default.

sudo nano /etc/mime.types

> application/gzip gz tgz

sudo nano /etc/httpd/conf.modules.d/00-base.conf

> LoadModule deflate_module modules/ > ... > LoadModule headers_module modules/ > ...

Because Apache will compress with every request, it is a better configuration to pre-compress and serve (without re-compressing).

Configure Expires on Apache

Should load mod_expires in the base conf:

nano /etc/httpd/conf.modules.d/00-base.conf

> LoadModule expires_module modules/

Then edit the httpd.conf

nano /etc/httpd/conf/httpd.conf
/// enable and add default expires

The Use and Abuse of CDNs

The big problem with CDNs, besides relying on a third-party service provider (another point of failure), is that it tends to break several things, or requires complex (and expensive) use of shared SSL certificates. By definition it is the use of third-party servers... or is it?

And this is how CDNs can become a part of the solution, that is an in-house, roll-your-own CDN. This makes perfect sense when using part of the awesome Linode multi-homed, multi-peered network. It makes sense to have a Singapore or Tokyo Asian HQ (being based in Asia, as I am) and CDNs in San Francisco and London. - (To be written) - Roll-your-own CDN on CentOS and Apache or Amazon Cloudwatch CDN