Caddy: JSON served with incorrect mime by default

Created on 19 Aug 2016  路  11Comments  路  Source: caddyserver/caddy

(Are you asking for help with using Caddy? Please use our forum instead: https://forum.caddyserver.com. If you are filing a bug report, please answer the following questions. If your issue is not a bug report, you do not need to use this template. Either way, please consider donating if we've helped you. Thanks!)

1. What version of Caddy are you running (caddy -version)?

v0.9.1

2. What are you trying to do?

Serving a JSON file with the .json extension.

3. What is your entire Caddyfile?

:80

ext .html .html .php
gzip

log logs/requests.log {
    rotate {
        size 50
        age  30
        keep 5
    }
}

errors {
    log logs/errors.log {
        size 50
        age  30
        keep 5
    }
}

4. How did you run Caddy (give the full command and describe the execution environment)?

FROM alpine:latest

EXPOSE 80

RUN mkdir -p /srv/app/
WORKDIR /srv/app/

# Install caddy

RUN apk add --no-cache curl tar && \
    curl "https://caddyserver.com/download/build?os=linux&arch=amd64" | \
    tar --no-same-owner -C /usr/bin/ -xz caddy

# Copy source in the last step as the source might change the most

COPY . /srv/app/

VOLUME "/srv/app/logs/"

ENTRYPOINT ["/usr/bin/caddy"]
CMD ["--conf", "/srv/app/Caddyfile"]

5. What did you expect to see?

JSON served with Content-Type:application/json

6. What did you see instead (give full error messages and/or log)?

JSON served with content-type:text/plain; charset=utf-8. It works fine when using mime .json application/json, but shouldn't Content-Type:application/json be the default for JSON files?

7. How can someone who is starting from scratch reproduce this behavior as minimally as possible?

Build the given Dockerfile.

documentation

Most helpful comment

Just in case anyone finds this in the future - I've discovered the easiest way to resolve this issue is to have /etc/mime.types be present in the Docker image you're running Caddy in. The easiest way to do this (with an Alpine based image) is to add this to your Dockerfile which solves the Content-Type issue for me:

RUN apk --no-cache add mailcap

All 11 comments

Caddy doesn't set Content-Types for you except when it is serving static files, which it does content sniffing to try to detect the content-type. The Go standard library does not appear to detect JSON files.

This is not a bug, but could be a feature request. However, sniffing content by default for everything is going to slow down the server. That, and it may be wrong. Site owners serving dynamic sites should probably just set the Content-Type for JSON files like you do with other web servers.

What extensions are counted as static files? The JSON file is saved on disk and completely static in my case.

I tried to find out when Caddy sets the Content-Type automatically, but I couldn't find it in the docs. Adding this information to the mime docs might be a good idea. I'm not sure if I need to set mime for ofter types as well, without knowing where sniffing won't happen.

Well, like I said:

The Go standard library does not appear to detect JSON files.

So you have to use the mime directive to set the Content-Type.

A good rule of thumb in general for web develoment is, where you care about the content-type, make sure you set it yourself.

Sure, that's fine now as I know it. The last question was more about the fact that the Caddy docs only say that it tries to detect the correct Content-Type, but there's no word left when that's the case. Adding or linking some details where this happens would be helpful.

Gotcha. Yeah we should probably improve that.

Change made locally, will push to site on next deploy. Thanks!

To get the correct mime type I have installed the mailcap package on my alpine machine.

For anyone interested, I converted a nginx mime.types file to work with the Caddy mime directive. It should contain all relevant mime types: https://gist.github.com/electerious/3d5a31a73cfd6423f835c074ab25fc06

Just in case anyone finds this in the future - I've discovered the easiest way to resolve this issue is to have /etc/mime.types be present in the Docker image you're running Caddy in. The easiest way to do this (with an Alpine based image) is to add this to your Dockerfile which solves the Content-Type issue for me:

RUN apk --no-cache add mailcap

As already written 2 years ago...

Just want to add that adding the mailcap file to the Docker image was the only solution I found to a strange issue where forcing the type to application/json through the mime directive was not enough when templates was also enabled ! As reported here : https://github.com/mholt/caddy/issues/2602

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SteffenDE picture SteffenDE  路  3Comments

PhilmacFLy picture PhilmacFLy  路  3Comments

kilpatty picture kilpatty  路  3Comments

jgsqware picture jgsqware  路  3Comments

aeroxy picture aeroxy  路  3Comments