Minify: Auto Compress and Combine JS and CSS Files in Nginx

Minify JS and CSS in Nginx

Pull the latest version of Minify library from https://github.com/mrclay/minify

Copy the “min” directory to the document root (generally www or public_html)

Add the following variables to min/config.php if you planned to keep “min” folder somewhere else:

// Set the document root to be the path of the "site root"
$min_documentRoot = '/path/to/root/directory/for/your/domain'; 
// Set $sitePrefix to the path of the site from the webserver's real docroot
list($sitePrefix) = explode('/min/index.php', $_SERVER['SCRIPT_NAME'], 2);
// Prepend $sitePrefix to the rewritten URIs in CSS files
$min_symlinks['//' . ltrim($sitePrefix, '/')] = $min_documentRoot;
Add the following code into your nginx configuration:
rewrite  ^/min/([a-z]=.*) /min/index.php?$1  last;
The same result can be obtained by using two different perl libraries for CSS and JS. THe instructions to setup and configuration can be found at wiki.nginx.org/EmbeddedPerlMinifyJS.

Join the Conversation

3 Comments

  1. Hi Indude, Thanks for this – saved me some time. You missed a parenthesis in the rewrite. Good luck surfing!

Leave a comment

Your email address will not be published. Required fields are marked *