Skip to content

Instantly share code, notes, and snippets.

@fizzyade
Last active March 25, 2024 22:58
Show Gist options
  • Save fizzyade/8b7978c9001c9dde987c16bdfa322a01 to your computer and use it in GitHub Desktop.
Save fizzyade/8b7978c9001c9dde987c16bdfa322a01 to your computer and use it in GitHub Desktop.
An example of a Caddy 2 JSON configuration file for a reverse proxy that uses the Cloudflare DNS module
{
  "apps": {
    "http": {
      "servers": {
        "example": {
          "listen": [
            ":80",
            ":443"
          ],
          "routes": [
            {
              "match": [
                {
                  "host": [
                    "mydomain.co.uk"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "handler": "reverse_proxy",
                          "upstreams": [
                            {
                              "dial": "localhost:8080"
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    },
    "tls": {
      "automation": {
        "policies": [
          {
            "subjects": [
              "mydomain.co.uk"
            ],
            "issuer": {
              "challenges": {
                "dns": {
                  "provider": {
                    "api_token": "<api token goes here>",
                    "name": "cloudflare"
                  }
                }
              },
              "email": "<email address used for cloudflare account>",
              "module": "acme"
            }
          }
        ]
      }
    }
  }
}
@sae13
Copy link

sae13 commented Dec 2, 2021

{"error":"loading config: loading new config: loading http app module: provision http: getting tls app: loading tls app module: decoding module config: tls: json: unknown field \"issuer\""}


@yurenchen000
Copy link

yurenchen000 commented Feb 3, 2022

@sae13
seems issuer: { ... } format is gone,
replaced by issuers:[ {...} ]

and
api_token may auth_token

@yurenchen000
Copy link

yurenchen000 commented Feb 3, 2022

// can't find a demo in offical docs
after litte try, here is a v2 demo.json:

  • run https at port :3443
    • file brower
  • with tls issuers by dns
{
  "admin": {
    "disabled": true
  },
  "apps": {
    "http": {
      "servers": {
        "static": {
          "automatic_https": {
            "disable_redirects": true
          },
          "idle_timeout": 30000000000,
          "listen": [
            ":3443"
          ],
          "max_header_bytes": 10240,
          "read_header_timeout": 10000000000,
          "routes": [
            {
              "handle": [
                {
                  "browse": {},
                  "handler": "file_server",
                  "root": "/home/chen/down/"
                }
              ],
              "match": [
                {
                  "host": [
                    "your.domain.com"
                  ]
                }
              ]
            }
          ]
        }
      }
    },
    "tls": {
      "automation": {
        "policies": [
          {
            "subjects": [
              "your.domain.com"
            ],
            "issuers": [
              {
                "module": "acme",
                "challenges": {
                  "dns": {
                    "provider": {
                      "name": "dnspod",
                      "auth_token": "id,token"
                    }
                  }
                }
              }
            ]
          }
        ]
      }
    }
  }
}

@yurenchen000
Copy link

json format is not easy for manual.
so here is a caddyfile:

  • https @ :3443
  • file browser @ /public
# global conf
{
    auto_https disable_redirects
    #acme_dns dnspod your_id,your_token
}

# server
your.domain.com:3443 {
    #root * /srv/http

    tls {
        dns dnspod your_id,your_token
    }

    handle /public/* {
        root ./down
        uri strip_prefix /public
        file_server browse
    }

    file_server
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment