Skip to content

v2: Config from Scratch

Matt Holt edited this page Sep 10, 2019 · 7 revisions

An empty config does nothing:

{}

This is an empty HTTP server, which responds on port 80 to any host, but with an empty response:

{
	"apps": {
		"http": {
			"servers": {
				"myserver": {
					"listen": [":80"]
				}
			}
		}
	}
}

This is a static file server out of the the /var/www directory:

{
	"apps": {
		"http": {
			"servers": {
				"myserver": {
					"listen": [":80"],
					"routes": [
						{
							"handle": [{
								"handler": "file_server",
								"root": "/var/www"
							}]
						}
					]
				}
			}
		}
	}
}

And this is the same, but with automatic HTTPS (notice that we now serve on the HTTPS port; any port will do, as long as it's not the HTTP port):

{
	"apps": {
		"http": {
			"servers": {
				"myserver": {
					"listen": [":443"],
					"routes": [
						{
							"match": [{"host": ["example.com"]}],
							"handle": [{
								"handler": "file_server",
								"root": "/var/www"
							}]
						}
					]
				}
			}
		}
	}
}

The reason this particular config gets automatic HTTPS is because the host matcher informs Caddy which hostnames to manage certificates for.

The following is a simple HTTP reverse proxy, with automatic HTTPS disabled:

{
	"apps": {
		"http": {
			"servers": {
				"example": {
					"listen": ["127.0.0.1:80"],
					"routes": [
						{
							"match":[{"host":["a.myhost.com"]}],
							"handle": [
								{
									"handler": "reverse_proxy",
									"upstreams": [
										{
											"dial": "localhost:8080"
										}
									]
								}
							]
						},
						{
							"match":[{"host":["b.myhost.com"]}],
							"handle": [
								{
									"handler": "reverse_proxy",
									"upstreams": [
										{
											"dial": "localhost:3000"
										}
									]
								}
							]
						}
					],
					"automatic_https":{
						"disable": true
					}
				}
			}
		}
	}
}
You can’t perform that action at this time.