简体   繁体   中英

minify html, css and js on nginx

I was looking for a good way to minifying my css, js and html codes, and found this package at google https://code.google.com/p/minify/ . The issue that I have Nginx web server where this minifying application needs mod_rewrite which comes with Apache only. I got this message when I ran the script:

Your webserver does not seem to support mod_rewrite (used in /min/.htaccess). Your Minify URIs will contain "?", which may reduce the benefit of proxy cache servers.

Now I want to know if there is a way I can use this script on my Nginx server or not? if not, then what would be the alternative to that??

I'm looking for minifying css, js and html that make my web pages fast enough so that my clients can browse my site pages quickly...

any idea?

Thanks

Update #1:

I just found out that I had to add a rewrite rule (replacing .htaccess rule) on my nginx server to redirect the folder and its contents.

    location / {  

                rewrite ^/min/([a-z]=.*) /min/index.php?$1 last;
}

but that redirects to error 404 ... any idea what the correct code is??

The way you have it is actually correct, the issue that you are having is likely the same one I am (and I'm not sure why it is) but basically it comes down to NGINX's rewrite rules ignoring the ? next to the $1 in the rule.

A work around for this is simply instead of going to example.com/min/f=path/to/file.css just put a ? in front of the f example.com/min/?f=path/to/file.css.

A better method would be to just serve the files as a group:

For the best performance you can serve these files as a pre-defined group with a URI like: /min/g=keyName

To do this, add a line like this to /min/groupsConfig.php:

return array(
    'keyName' => array('//path/to/js/file.js', '//path/to/js/otherfile.js')
);

Chances are though, you may need to use /min/?g=keyName.

As a side note, minifying and bundling isn't just ~1kb it can (and tends to be) much more. It has a huge impact on the user (especially on mobile devices). A browser can make 6 concurrent connections, so if you have any more files than that being downloaded, the user is waiting for them, one of the projects I recently have been working on had roughly 60 requests being made for different js and css files (the original coders were... all inclusive in the plugins department). The entire page was roughly 1 Meg and took 3 seconds to download uncached (nothing was cached, because the previous coders don't understand caching). I minified bundled and compressed everything into 3 files (removed the useless stuff too) and got the entire page down to 20kb uncached, 3kb cached, with an uncached load time under 20ms.

That was an extreme example of poor coding though. One final thought... if you don't go into the config and add the cache directories and cache everything, it will cause a slight performance hit on the server (though, probably not as severe as serving up a dozen extra files). I suggest enabling APC or memcache, or at least specifying the cache folder for it to store the files in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM