(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!)
caddy -version)?v0.9.1
Serving a JSON file with the .json extension.
: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
}
}
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"]
JSON served with Content-Type:application/json
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?
Build the given Dockerfile.
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
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.typesbe 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: