iTranslated by AI
Caddy: Specifying 'localhost' in Caddyfile Prevents Access via https://127.0.0.1/
Environment
- XUbuntu 18.04
- caddy v2.4.5
Introduction
I am using a web server called Caddy.
I start caddy using the following Caddyfile.
localhost
respond "Hello, world!"
https://localhost can be accessed, but https://127.0.0.1/ cannot.
Why is this?
$ curl https://localhost
Hello, world!$
$ curl https://127.0.0.1/
curl: (35) error:14094438:SSL routines:ssl3_read_bytes:tlsv1 alert internal error
Cause
The documentation at https://caddyserver.com/docs/caddyfile/concepts#addresses states the following:
If you specify a hostname, only requests with a matching Host header will be honored. In other words, if the site address is localhost, then Caddy will not match requests to 127.0.0.1.
It seems that access to https://127.0.0.1/ is not possible because it checks whether the Host request header matches.
You can access both https://localhost and https://127.0.0.1/ by specifying multiple hosts separated by commas as follows:
localhost, 127.0.0.1
respond "Hello, world!"
$ curl https://127.0.0.1/
Hello, world!$ curl https://localhost/
Hello, world!$
Note
When specifying multiple hosts, a space is required after the comma.
If there is no space after the comma, the following error occurs:
$ sudo caddy run -config Caddyfile
2021/09/20 01:00:29.692 INFO using provided configuration {"config_file": "Caddyfile", "config_adapter": ""}
run: adapting config using caddyfile: Caddyfile:1 - Error during parsing: Site addresses cannot contain a comma ',': 'localhost,127.0.0.1' - put a space after the comma to separate site addresses
Discussion