Thanks @Whitestrake.
Thanks so much! I got your example working. I still have two more questions if you can spare the time. I really appreciate your help, so much!
I’m using the current master 5c55e5d53f
for Caddy. Here’s my process:
$ mkdir -p /tmp/caddy/cmd/caddy/www
$ echo "test html!" | gzip > /tmp/caddy/cmd/caddy/www/index.html
$ cd /tmp/caddy/cmd/caddy && go build
$ vim Caddyfile #make changes
$ ./caddy run -config ./Caddyfile
Here is the Caddyfile I use which works (following yours):
http://:8080 {
root www
file_server
@gz {
file {
try_files {uri}.gz {uri}/index.html.gz
}
}
route @gz {
rewrite {http.matchers.file.relative}
header Content-Type text/html
header Content-Encoding gzip
}
}
This Caddyfile successfully serves index.html.gz
from localhost:8080
in the browser.
How can I control the Content-Type
to follow the content type?
I found that If I don’t have that header Content-Type text/html
line, then the browser won’t show the file, it will revert to downloading it instead. I’d like to be able to server gzipped css and js as well, which means the content type will change. Please let me if you have any ideas about how to serve any gzipped content (html / css / js).
Can I use an absolute path as a root with file_server
?
Instead of using root www
I’d like to use the absolute path, root /tmp/caddy/cmd/caddy/www
. However, this Caddyfile won’t run:
http://:8080 {
root /tmp/caddy/cmd/caddy/www
file_server
@gz {
file {
try_files {uri}.gz {uri}/index.html.gz
}
}
route @gz {
rewrite {http.matchers.file.relative}
header Content-Type text/html
header Content-Encoding gzip
}
}
It gives an error:
$ ./caddy run -config ./Caddyfile
2020/03/26 14:34:37.975 INFO using provided configuration {"config_file": "./Caddyfile", "config_adapter": ""}
run: adapting config using caddyfile: parsing caddyfile tokens for 'root': ./Caddyfile:3 - Error during parsing: Wrong argument count or unexpected line ending after 'root'
So instead I try using the file_server
directive:
http://:8080 {
file_server * {
root /tmp/caddy/cmd/caddy/www
}
@gz {
file {
try_files {uri}.gz {uri}/index.html.gz
}
}
route @gz {
rewrite {http.matchers.file.relative}
header Content-Type text/html
header Content-Encoding gzip
}
}
and that runs in Caddy, but it serves only 404’s now.
Actually, this directive doesn’t work with the relative path either:
http://:8080 {
file_server * {
root www
}
@gz {
file {
try_files {uri}.gz {uri}/index.html.gz
}
}
route @gz {
rewrite {http.matchers.file.relative}
header Content-Type text/html
header Content-Encoding gzip
}
}
as it also gives 404s. I did not expect this behavior, according to the docs. Please let me know if I’m doing something wrong!