Minify JS and CSS in Nginx

Pull the latest version of Minify library from

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;

That's it.

The same result can be obtained by using two different perl libraries for <a href="http://search.cpan.org/~pmichaux/CSS-Minifier-0.01/">CSS</a> and <a href="http://search.cpan.org/~pmichaux/JavaScript-Minifier/">JS</a>. THe instructions to setup and configuration can be found at <a href="http://wiki.nginx.org/EmbeddedPerlMinifyJS">wiki.nginx.org/EmbeddedPerlMinifyJS</a>.

Category Web Devlopment | Tags:

Leave a reply