iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙆

Caddy: Serve local files at https://localhost/xxx

に公開

Introduction

I want to make local files accessible via a URL such as https://localhost/test.jpg.
To set up the environment as easily as possible, I used a web server called Caddy.

What is Caddy

It is an open-source web server written in the Go language.
https://caddyserver.com/

Caddy enables HTTPS by default. I used Caddy because I found this feature convenient.

Caddyfile

Caddy configurations are written in a file called Caddyfile.

Caddyfile
# Hostname is localhost
localhost
# Serve the /tmp/www directory as a file server
root * /tmp/www
file_server {
	# Display the file list
	browse
}

The contents of /tmp/www are as follows:

$ ls /tmp/www
hello.txt
$ cat /tmp/www/hello.txt 
hello world

Starting Caddy

Use the caddy run command to start the web server.

$ sudo caddy run -config Caddyfile 
2021/09/20 05:21:29.207	INFO	using provided configuration	{"config_file": "Caddyfile", "config_adapter": ""}
2021/09/20 05:21:29.209	INFO	admin	admin endpoint started	{"address": "tcp/localhost:2019", "enforce_origin": false, "origins": ["localhost:2019", "[::1]:2019", "127.0.0.1:2019"]}
2021/09/20 05:21:29.209	INFO	http	server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS	{"server_name": "srv0", "https_port": 443}
2021/09/20 05:21:29.210	INFO	http	enabling automatic HTTP->HTTPS redirects	{"server_name": "srv0"}
2021/09/20 05:21:29.209	INFO	tls.cache.maintenance	started background certificate maintenance	{"cache": "0xc0001dcc40"}
2021/09/20 05:21:29.211	INFO	tls	cleaning storage unit	{"description": "FileStorage:/home/vagrant/.local/share/caddy"}
2021/09/20 05:21:29.211	INFO	tls	finished cleaning storage units
2021/09/20 05:21:29.229	INFO	pki.ca.local	root certificate is already trusted by system	{"path": "storage:pki/authorities/local/root.crt"}
2021/09/20 05:21:29.229	INFO	http	enabling automatic TLS certificate management	{"domains": ["localhost"]}
2021/09/20 05:21:29.229	WARN	tls	stapling OCSP	{"error": "no OCSP stapling for [localhost]: no OCSP server specified in certificate"}
2021/09/20 05:21:29.229	INFO	autosaved config (load with --resume flag)	{"file": "/home/vagrant/.config/caddy/autosave.json"}
2021/09/20 05:21:29.229	INFO	serving initial configuration

Running in the Background

caddy run command starts the process in the foreground. To start the process in the background, execute the caddy start command.

Verification

You can access https://localhost/hello.txt to retrieve the contents of the file.

$ curl https://localhost/hello.txt
hello world

Also, accessing https://localhost displays a list of files under /tmp/www.

Supplementary Notes

Adding response headers

You can add response headers using the header directive.

Caddyfile
header {
    Access-Control-Allow-Origin: https://example.com
}

Checking Caddy settings

You can check the current Caddy settings by accessing http://localhost:2019/config/ [1]. If the behavior is not as described in the Caddyfile, I think it's best to check the settings this way first.

https://caddyserver.com/docs/api#get-configpath

$ curl http://localhost:2019/config/ | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   277  100   277    0     0  69250      0 --:--:-- --:--:-- --:--:-- 69250
{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":443"
          ],
          "routes": [
            {
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "handler": "vars",
                          "root": "/tmp/www"
                        },
                        {
                          "browse": {},
                          "handler": "file_server",
                          "hide": [
                            "./Caddyfile"
                          ]
                        }
                      ]
                    }
                  ]
                }
              ],
              "match": [
                {
                  "host": [
                    "localhost"
                  ]
                }
              ],
              "terminal": true
            }
          ]
        }
      }
    }
  }
}

Cannot access https://127.0.0.1/

In most environments, localhost is the same as 127.0.0.1, so it seems like you could access it via https://127.0.0.1/, but in Caddy's case, you cannot. This is because it verifies the Host request header.
For more details, please refer to https://zenn.dev/yuji38kwmt/articles/5d3e729dc9a5c1.

脚注
  1. A trailing slash is required at the end of the URL. You cannot access it via http://localhost:2019/config. ↩︎

GitHubで編集を提案

Discussion