Skip to content

Instantly share code, notes, and snippets.

@tlongren
Last active May 6, 2019 01:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tlongren/529028608af9f7fac083327027f1af05 to your computer and use it in GitHub Desktop.
Save tlongren/529028608af9f7fac083327027f1af05 to your computer and use it in GitHub Desktop.
Output Compression, Browser Caching, Asset Minification and Image Optimization for WordPress

Output Compression, Browser Caching, Asset Minification and Image Optimization

The following apache and nginx configurations will handle compression and browser caching. A few different WordPress plugins can be used for asset minification.

mod-pagespeed may be available for configuration as an apache or nginx module. If available, there's no need to use the individual apache or nginx configurations. Mod-pagespeed will handle compression and browser caching, making the apache and nginx configurations unnecessary. The WordPress plugins are still needed for asset minification as they ensure assets are loaded in the proper order once minified.

Image optimization should be done using the EWWW Image Optimizer plugin.

Apache Config

Output Compression

RewriteEngine On

#Begin gzip and deflate
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css application/x-javascript text/plain text/xml image/x-icon
</IfModule>

Browser Caching

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

Nginx Config

Output Compression

server {
  gzip on;
  gzip_types text/html text/css application/x-javascript text/plain text/xml image/x-icon;
}

Browser Caching

location ~* \.(jpg|png|gif|jpeg|css|js)$ {
  expires 50h;
}

Asset Minification Plugins

  1. Breeze
  2. WP Super Minify
  3. WP Roids
  4. Merge + Minify + Refresh

mod-pagespeed

mod-pagespeed for Apache

<IfModule pagespeed_module>
  ModPagespeed on
  ModPagespeedEnableFilters rewrite_css,combine_css
  ModPagespeedEnableFilters recompress_images
  ModPagespeedEnableFilters convert_png_to_jpeg,convert_jpeg_to_webp
  ModPagespeedEnableFilters collapse_whitespace,remove_comments
</IfModule>

mod-pagespeed for nginx

server {
  pagespeed on;
  pagespeed EnableFilters rewrite_css,combine_css;
  pagespeed EnableFilters rewrite_images;
  pagespeed EnableFilters collapse_whitespace,remove_comments;
}

Everything for Plesk

# MOD_DEFLATE COMPRESSION
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript application/x-httpd-php
DeflateCompressionLevel 9
<IfModule mod_expires.c>
	ExpiresActive On
	ExpiresByType image/jpg "access 1 year"
	ExpiresByType image/jpeg "access 1 year"
	ExpiresByType image/gif "access 1 year"
	ExpiresByType image/png "access 1 year"
	ExpiresByType text/css "access 1 month"
	ExpiresByType text/html "access 1 month"
	ExpiresByType application/pdf "access 1 month"
	ExpiresByType text/javascript "access 1 month"
	ExpiresByType application/x-javascript "access 1 month"
	ExpiresByType image/x-icon "access 1 year"
	ExpiresDefault "access 1 month"
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment