I'm using nginx reverse proxy cache with gzip enabled. However, I got some problems from Android applications HTTP-requests to my Rails JSON web service. It seems when I turn off reverse proxy cache, it works ok because the response header comes without gzip. Therefore, I think the problem is caused by gzip. What is the most appropriate level of gzip compression?
gzip on;
gzip_http_version 1.0;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/css text/javascript application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss;
I tested this under nginx 1.3.9 with two files, and these were the results I got for the various levels:
text/html
- phpinfo():application/x-javascript
- jQuery 1.8.3 (Uncompressed):I'm not sure how representative this is but it should serve as an example. Also, I haven't taken the CPU usage into account but from these results the ideal compression level seems to be between
4
and6
.Additionally, if you use the
gzip_static
module, you may want to pre-compress your files (in PHP):This allows you to get the best possible compression without sacrificing the CPU on every request.
The level of gzip compression simply determines how compressed the data is on a scale from 1-9, where 9 is the most compressed. The trade-off is that the most compressed data usually requires the most work to compress/decompress, so if you have it set fairly high on a high-volume website, you may feel its effect.
It sounds like your issues are more related to the HTTP headers on the requests. Usually gzip-compressed HTTP traffic is accompanied by the
Content-Encoding: gzip
header. If this is being dropped somewhere, then the client might not know to have to decompress the response.If you really can spare CPU resources, you can use 9, but for most sites a value of 2 is enough, since gzip doesn't reduce the file much after level 1.
Edit: I looked at Amazon CloudFront and it seems to be using level 6, probably because that level is the one that runs decompression faster, thus improving page render performance.
If you have high volume website and still would like to have a full level (9) of compression, the best idea would be to put your static content on Amazon S3 or similar object storage services and upload the compressed files.
You still would want to use nginx to compress your HTML, so better to keep that value to normal, I use 5 there.