Nginx Requests Not Being Gzipped on CDN Pass-Through

A couple months ago I ran into a curious situation. Requests from Nginx were being gzipped as expected and requests from CDN were being gzipped. My Nginx settings for compression looked like this:


gzip on;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 0;
gzip_types application/json application/x-javascript application/javascript text/plain text/css text/javascript text/xml;
gzip_vary on;
gzip_disable "MSIE [1-6]\."; 

Reproducing the behavior was simple enough. If you hit the URI at CDN and append a cache busting string to the end of the URI such as \?2342343243 the headers would return without the Content-Encoding: gzip header. Another test to confirm this was to use a curl statement passing the via header with any value such as below

 

curl -v -H “Accept-Encoding: gzip” -H “Via: 1.1 akamai.net(ghost) (AkamaiGHost)”  "http://youruri.com” 

A simple explanation of this issue is that when request hits the CDN and the object is not cached, the request is then passed to origin using the via header. Unless the Nginx gzip directive of gzip_proxied any; is included this request will always be sent uncompressed. To resolve add this line to your nginx.conf file. Below is the same example shown earlier but including this directive:


gzip  on;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 0;
gzip_types application/json application/x-javascript application/javascript text/plain text/css text/javascript text/xml;
gzip_proxied any;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";

Leave a Reply

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

2 × 2 =