Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can someone help me with a Perl or dynamic module to strip of whitespaces & comments from html responses in nginx?

share|improve this question
Could you please explain your case a little more? – quanta Oct 26 '11 at 6:29
my html contains a lot of whitespace noise and html comments. This adds up to payload. if you look at you'll see that google is stripping the whitespace noise. Wanted to achieve something similar. – Quintin Par Oct 26 '11 at 6:43
Are there any specific problems in rebuilding nginx? Lack of permissions or knowledge? – Cyclops Nov 4 '11 at 12:28

This should be done at deploy time as a preprocessing step, otherwise you will be wasting cpu cycles each time you serve content.

share|improve this answer

Looks like there's a module to strip whitespace (not comments apparently)

share|improve this answer
But this requires rebuilding of Nginx, which he tries to avoid. – faker Oct 31 '11 at 15:52

As Ochoto already said, this should be done at deploy, or better, at render time. Because sometimes you might have spaces (non- ) which are needed in order to display the page correctly. If you do this at a later stage, you can run into troubles (it can also work)

Anyway, if payload-size is your issue, have a look at compression. This will produce far better results in my opinion.

share|improve this answer

This can be extremely tricky!

You should not only be aware of <pre> tags, but consider stuff like this:

<svg xmlns="" version="1.1">
   <text x="0" y="15" xml:space="preserve">hello     world</text>

Your post-processor would really need to parse the content and understand it. A simple regular expression will surely mess things up sooner or later.

As Cohoto and Franz Bettag have mentioned, this should aether be done in the source or as a pre-deployment step and not as a post-processing fix-up.

For javascript there are some obfuscators or compressors available that safely remove whitespace, parse the script and shorten all variables to single letters etc, knowing what they are doing since they know the context. However I've yet to see a know-it-all xhtml compressor that implements all that an average browser can handle. It is possible to build one that ignores anything it does not recognize though, but would it really be worthwhile?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.