Simplifying encode content types

The encode directive for zstd/gzip compression matches on a list of content types. The defaults are shown in the documentation.

If you want to add additional content types to this list you have to cut and paste the entire list in your matcher to include the default ones and so it ends up looking like the below just because I’ve added two new types. This also means that I have to keep an eye on the defaults in case they change.

Is there any way to simplify this, or could it maybe be a feature request to do something like prefix them with + to indicate they get added to the defaults rather than replace the defaults?

encode zstd gzip {
        match {
                header Content-Type application/atom+xml*
                header Content-Type application/eot*
                header Content-Type application/font*
                header Content-Type application/geo+json*
                header Content-Type application/graphql+json*
                header Content-Type application/javascript*
                header Content-Type application/json*
                header Content-Type application/ld+json*
                header Content-Type application/manifest+json*
                header Content-Type application/opentype*
                header Content-Type application/otf*
                header Content-Type application/rss+xml*
                header Content-Type application/truetype*
                header Content-Type application/ttf*
                header Content-Type application/vnd.api+json*
                header Content-Type application/vnd.ms-fontobject*
                header Content-Type application/wasm*
                header Content-Type application/x-httpd-cgi*
                header Content-Type application/x-javascript*
                header Content-Type application/x-opentype*
                header Content-Type application/x-otf*
                header Content-Type application/x-perl*
                header Content-Type application/x-protobuf*
                header Content-Type application/x-ttf*
                header Content-Type application/xhtml+xml*
                header Content-Type application/xml*
                header Content-Type font/ttf*
                header Content-Type font/otf*
                header Content-Type image/svg+xml*
                header Content-Type image/vnd.microsoft.icon*
                header Content-Type image/x-icon*
                header Content-Type multipart/bag*
                header Content-Type multipart/mixed*
                header Content-Type text/*

                header Content-Type application/feed+json*
                header Content-Type application/gpx+xml*
        }
}

Actually you can put them on one line, though our docs don’t make this clear:

match header Content-Type application/atom+xml* application/eot* application/font* ...

If you want to have them on multiple lines…

match header Content-Type \
    application/atom+xml* \
    application/eot* \
    ...

Something like that.

1 Like

OK. That simplifies it a little bit. But that still means listing every one from the default list. I was hoping there could maybe be some way of just doing “include defaults” + “my own type 1” + “my own type 2” so we don’t have to replicate the default list at all.

I know this won’t actually work because the + in the header directive doesn’t do this, but something like match header +Content-Type application/one application/two where the + sign makes it append to the default list rather than overwriting it.

What do you mean by “my own type”? Why not just put them all in the list together? It’s all your list after all, it is your config.

1 Like

I mean the two additional types I added to the list, the two at the bottom of my original post for application/feed+json (my own type 1) and application/gpx+xml (my own type 2).

My point really is I only want to have those two listed in my config file rather than double the size of my Caddyfile with all of the 34 default ones as well, but I still want the default ones to be applied.

I know there is no way to do this right now, but I was wondering if there might have been a way to do that to keep the size of the Caddyfile compact and neat and without needing to keep that default list updated whenever there is a new Caddy version that may have changed it.

I’ve realised that I can import from other files so I’ve simply put the list of default content-types from the Caddy source code into another file and then imported it into my main Caddyfile. This keeps my Caddyfile compact and easy to read so I’m happy with this. It still means that I have to occasionally check the Caddy source code to see if that default list has changed at all, but that’s not a big problem.

encode zstd gzip {                                                                                                                                                 
        match {                                                                                                                                                    
                import Caddyfile.gzip                                                                                                                              
                                                                                                                                                                           
                header Content-Type application/feed+json*                                                                                                         
                header Content-Type application/gpx+xml*                                                                                                           
        }                                                                                                                                                          
}
1 Like