Skip to content

Instantly share code, notes, and snippets.

@SimJoSt
Created November 28, 2024 04:43
Show Gist options
  • Save SimJoSt/4df205f5c76197cd0e17e97a13f28d91 to your computer and use it in GitHub Desktop.
Save SimJoSt/4df205f5c76197cd0e17e97a13f28d91 to your computer and use it in GitHub Desktop.
Caddy Caddyfile config adapted to JSON with wildcard site with force_automate tls option
{
"logging": {
"logs": {
"default": {
"writer": {
"filename": "/home/deployer/caddy/caddy.log",
"output": "file"
},
"encoder": {
"format": "console"
},
"exclude": [
"http.log.access.log0",
"http.log.access.log1"
]
},
"log0": {
"writer": {
"filename": "/home/deployer/apps/app1/log/access.log",
"output": "file"
},
"include": [
"http.log.access.log0"
]
},
"log1": {
"writer": {
"filename": "/home/deployer/apps/app2/log/access.log",
"output": "file"
},
"include": [
"http.log.access.log1"
]
}
}
},
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":443"
],
"routes": [
{
"match": [
{
"host": [
"app1.com"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "vars",
"root": "/home/deployer/apps/app1/current/"
},
{
"handler": "headers",
"response": {
"require": {
"headers": {
"Cache-Control": null
}
},
"set": {
"Cache-Control": [
"max-age=31536000"
]
}
}
},
{
"encodings": {
"gzip": {},
"zstd": {}
},
"handler": "encode",
"prefer": [
"gzip",
"zstd"
]
}
]
},
{
"handle": [
{
"handler": "static_response",
"headers": {
"Location": [
"{http.request.orig_uri.path}/"
]
},
"status_code": 308
}
],
"match": [
{
"file": {
"try_files": [
"{http.request.uri.path}/index.php"
]
},
"not": [
{
"path": [
"*/"
]
}
]
}
]
},
{
"handle": [
{
"handler": "rewrite",
"uri": "{http.matchers.file.relative}"
}
],
"match": [
{
"file": {
"split_path": [
".php"
],
"try_files": [
"{http.request.uri.path}",
"{http.request.uri.path}/index.php",
"index.php"
]
}
}
]
},
{
"handle": [
{
"handler": "reverse_proxy",
"transport": {
"protocol": "fastcgi",
"resolve_root_symlink": true,
"split_path": [
".php"
]
},
"upstreams": [
{
"dial": "unix//run/php/php8.2-fpm.sock"
}
]
}
],
"match": [
{
"path": [
"*.php"
]
}
]
},
{
"handle": [
{
"handler": "file_server",
"hide": [
"/home/deployer/apps/app1/Caddyfile"
]
}
]
}
]
}
],
"terminal": true
},
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "vars",
"root": "/home/deployer/apps/app2/current/public"
}
]
},
{
"handle": [
{
"handler": "headers",
"response": {
"require": {
"headers": {
"Cache-Control": null
}
},
"set": {
"Cache-Control": [
"public, max-age=315360000"
]
}
}
}
],
"match": [
{
"path": [
"/build/assets/*"
]
}
]
},
{
"handle": [
{
"handler": "headers",
"response": {
"require": {
"headers": {
"Cache-Control": null
}
},
"set": {
"Cache-Control": [
"public, max-age=315360000"
]
}
}
}
],
"match": [
{
"path": [
"/static/*"
]
}
]
},
{
"handle": [
{
"handler": "static_response",
"headers": {
"Location": [
"{http.request.orig_uri.path}/"
]
},
"status_code": 308
}
],
"match": [
{
"file": {
"try_files": [
"{http.request.uri.path}/index.php"
]
},
"not": [
{
"path": [
"*/"
]
}
]
}
]
},
{
"handle": [
{
"handler": "rewrite",
"uri": "{http.matchers.file.relative}"
}
],
"match": [
{
"file": {
"split_path": [
".php"
],
"try_files": [
"{http.request.uri.path}",
"{http.request.uri.path}/index.php",
"index.php"
]
}
}
]
},
{
"handle": [
{
"handler": "reverse_proxy",
"transport": {
"protocol": "fastcgi",
"resolve_root_symlink": true,
"split_path": [
".php"
]
},
"upstreams": [
{
"dial": "unix//run/php/php8.2-fpm.sock"
}
]
}
],
"match": [
{
"path": [
"*.php"
]
}
]
},
{
"handle": [
{
"handler": "file_server",
"hide": [
"/home/deployer/apps/app2/Caddyfile"
]
}
]
}
]
}
],
"terminal": true
}
],
"errors": {
"routes": [
{
"match": [
{
"host": [
"app1.com"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"group": "group0",
"handle": [
{
"handler": "rewrite",
"uri": "/404.html"
}
],
"match": [
{
"expression": "{http.error.status_code} == 404"
}
]
},
{
"handle": [
{
"handler": "file_server",
"hide": [
"/home/deployer/apps/app1/Caddyfile"
],
"root": "/var/dep/html"
}
]
}
]
}
],
"terminal": true
},
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"group": "group1",
"handle": [
{
"handler": "rewrite",
"uri": "/404.html"
}
],
"match": [
{
"expression": "{http.error.status_code} == 404"
}
]
},
{
"handle": [
{
"handler": "file_server",
"hide": [
"/home/deployer/apps/app2/Caddyfile"
],
"root": "/var/dep/html"
}
]
}
]
}
],
"terminal": true
}
]
},
"tls_connection_policies": [
{
"match": {
"sni": [
"app1.com"
]
}
},
{
"certificate_selection": {
"any_tag": [
"cert0"
]
},
"default_sni": "coders.fail"
}
],
"logs": {
"default_logger_name": "log1",
"logger_names": {
"app1.com": [
"log0"
]
}
}
},
"srv1": {
"listen": [
":80"
],
"routes": [
{
"handle": [
{
"handler": "vars",
"root": "/usr/share/caddy"
},
{
"handler": "file_server",
"hide": [
"/etc/caddy/Caddyfile"
]
}
]
}
]
}
}
},
"tls": {
"certificates": {
"automate": [
"app2.com",
"*.app2.com",
"app1.com"
],
"load_files": [
{
"certificate": "/home/deployer/apps/app2/certs/cloudflare-origin-app2.com-cert.pem",
"key": "/home/deployer/apps/app2/certs/cloudflare-origin-app2.com-key.pem",
"tags": [
"cert0"
]
}
]
},
"automation": {
"policies": [
{
"subjects": [
"*.app2.com",
"app2.com",
"app1.com"
],
"issuers": [
{
"email": "mail@example.com",
"module": "acme"
},
{
"ca": "https://acme.zerossl.com/v2/DV90",
"email": "mail@example.com",
"module": "acme"
}
]
}
]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment