Gzip headers when using encode handler

1. Caddy version (caddy version):

v2.3.0 h1:fnrqJLa3G5vfxcxmOH/+kJOcunPLhSBnjgIvjXV/QTA=

2. How I run Caddy:

Docker Compose with Docker for Mac.

a. System environment:

OS X Big Sur 11.2.2 (20D80)

b. Command:

caddy run --config /etc/caddy/config.json --resume

c. Service/unit/compose file:

version: "3"

services:

  caddy:
    image: caddy
    command: [ 'caddy', 'run', '--config', '/etc/caddy/config.json', '--resume' ]
    ports:
      - 127.0.0.1:8080:80
      - 127.0.0.1:2019:2019
    volumes:
      - ./caddy/config.json:/etc/caddy/config.json

d. My complete Caddyfile or JSON config:

{
  "admin": {
    "disabled": false,
    "listen": "0.0.0.0:2019"
  },
  "apps": {
    "http": {
      "servers": {
        "sites": {
          "listen": [
            ":80"
          ],
          "routes": [
            {
              "group": "site_daa01c0b-8b74-4098-9107-05592ad89eae",
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "handler": "headers",
                          "response": {
                            "set": {
                              "Cache-Control": [
                                "public",
                                "max-age=0",
                                "must-revalidate"
                              ]
                            }
                          }
                        },
                        {
                          "encodings": {
                            "gzip": {
                              "level": 1
                            }
                          },
                          "handler": "encode",
                          "minimum_length": 0
                        },
                        {
                          "handler": "static_response",
                          "body": "Hi !"
                        }
                      ]
                    }
                  ]
                }
              ],
              "match": [
                {
                  "host": [
                    "main.localhost"
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  }
}

3. The problem I’m having:

When using the encode handle configured with gzip, I’m not seeing any Content-Encoding headers added to the response. The docs mention that an encoding is selected when the Accept-Encoding header is present. I’m assuming that when a single encoding is configured and no header is set, Caddy uses the only encoding configured.

4. Error messages and/or full log output:

5. What I already tried:

I thought maybe the handler was used in the wrong place, but in the docs it seems it’s always used before the file_server handler.

6. Links to relevant resources:

Setting minimum_length to 0 will cause it to use the default minimum length which is 512 bytes. If you want to “turn off” the minimum length, then you should set it to -1. But that’s not a good idea. Also, the default gzip level is set to 5. I recommend you stick with the defaults.

If you remove those two lines (minimum_length and level), then try again with a long enough static response body (over 512 bytes) then you should see the Content-Encoding header.

1 Like

Awesome, this seems to do the trick ! This should definitely be documented :slight_smile: Note however that:

  • 0 causing to be set to defaults is a bit strange IMO
  • -1 seems to behave differently than what you said

With min_length: 0:

Cache-Control: public,max-age=0,must-revalidate
Content-Length: 320
Content-Type: text/html; charset=utf-8
Date: Tue, 09 Mar 2021 16:17:03 GMT
Etag: "qppdxz8w"
Last-Modified: Tue, 09 Mar 2021 13:10:47 GMT
Server: Caddy

With min_length: -1, I see the Accept-Ranges: bytes which I wouldn’t expect:

Accept-Ranges: bytes
Cache-Control: public,max-age=0,must-revalidate
Content-Length: 320
Content-Type: text/html; charset=utf-8
Date: Tue, 09 Mar 2021 16:19:22 GMT
Etag: "qppdxz8w"
Last-Modified: Tue, 09 Mar 2021 13:10:47 GMT
Server: Caddy

With min_length: 1, I get the expected result:

Cache-Control: public,max-age=0,must-revalidate
Content-Encoding: gzip
Content-Length: 225
Content-Type: text/html; charset=utf-8
Date: Tue, 09 Mar 2021 16:20:40 GMT
Etag: "qppdxz8w"
Last-Modified: Tue, 09 Mar 2021 13:10:47 GMT
Server: Caddy
Vary: Accept-Encoding

Thanks so much for your help !

That’s because the field is an int, and the default value for ints in Go is 0, so checking for the default, as in “the user didn’t set something” is by looking for 0.

You’re right, my bad - setting minimum length to -1 will actually prevent any encoding from happening because there’s a rw.config.MinLength > 0 check – the Accept-Ranges header is deleted just before encoding happens.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.