My http block defines various gzip rules. Then in where I have pre-comporessed gzip files I’m using the gzip_static module. i.e. something like:

http {
    gzip on;
    server {
        #other vhost rules
        location /assets/ {
            gzip_static on;

For URLs that don’t start /assests nginx sends the header Content-Encoding: gzip. However URLs such as /assets/css/style.css certainly return a comporessed response, but no Content-Encoding header.

Is this expected behavior? How should nginx be configured?


Just turning it on with gzip_static on didn't work for me. I had to use the gzip_proxied before it started working.

location /as/ {
  gzip_static on;
  gzip_proxied    expired no-cache no-store private auth;

  gzip_min_length 500; # optional
  gzip_types      text/plain application/xml text/css; # optional

  add_header Z_LOCATION "gz static location block"; add_header URI $uri; # DEBUG info

Note the last line adds another two headers that help debugging. You can see these using a great plugin for Firefox called "Live HTTP Headers", which I found super useful for debugging a load of things around headers and caching. Note that "curl -I" doesn't work as it doesn't send a header saying it accepts gzip encoding.

You should also make sure the files have the same modification date

touch style.css style.css.gz

Here are the headers I get back when I use the location config above

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 06 Jan 2016 20:07:18 GMT
Content-Type: text/css
Last-Modified: Wed, 06 Jan 2016 19:55:15 GMT
Etag: "568d7123-84c"
Z_LOCATION: gz static location block
URI: /as/style.css
Via: 1.1 BC5-ACLD
Connection: Keep-Alive
Content-Encoding: gzip
Age: 0

I had a location ~* \.(css|js|...) { expires 7d } block that was messing things up. I’ve had a play with the location blocks and all seems to be working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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