Skip to content

v2: Caddyfile examples

Abdelmalek Ihdene edited this page Dec 13, 2019 · 8 revisions

Caddyfile configuration examples

Caddyfile configuration is easy and convenient, and is recommended for most manual workflows.

Overall, the v2 Caddyfile is actually the same as the old v1 Caddyfile, but some directives and their syntax have changed. We've also made significant improvements to request matching and setting global options. A guide for transitioning from v1 to v2 is coming soon.

Simple local static fileserver with php support

Configuring Caddy as a simple and local static HTTP fileserver that can also run PHP can look like this:

:8080

root * /path/to/webfolder
encode gzip
php_fastcgi php-fpm:9000
file_server

The converted equivalant (caddy adapt --config /path/to/Caddyfile --pretty) in JSON looks like this:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":8080"
          ],
          "routes": [{
            "handle": [{
              "handler": "subroute",
              "routes": [{
                "handle": [{
                    "handler": "vars",
                    "root": "/path/to/webfolder"
                  },
                  {
                    "handler": "subroute",
                    "routes": [{
                        "handle": [{
                          "encodings": {
                            "gzip": {}
                          },
                          "handler": "encode"
                        }]
                      },
                      {
                        "handle": [{
                          "handler": "rewrite",
                          "rehandle": true,
                          "uri": "{http.matchers.file.relative}{http.request.uri.query_string}"
                        }],
                        "match": [{
                          "file": {
                            "try_files": [
                              "{http.request.uri.path}",
                              "index.php"
                            ]
                          }
                        }]
                      },
                      {
                        "handle": [{
                          "handler": "reverse_proxy",
                          "transport": {
                            "protocol": "fastcgi",
                            "split_path": ".php"
                          },
                          "upstreams": [{
                            "dial": "php-fpm:9000"
                          }]
                        }],
                        "match": [{
                          "path": [
                            "*.php"
                          ]
                        }]
                      },
                      {
                        "handle": [{
                          "handler": "file_server",
                          "hide": [
                            "Caddyfile"
                          ]
                        }]
                      }
                    ]
                  }
                ]
              }]
            }]
          }]
        }
      }
    },
    "tls": {
      "automation": {},
      "session_tickets": {}
    }
  }
}

Simple FQDN static fileserver with php support and automatic HTTPS

Configuring Caddy as a simple and FQDN static HTTP fileserver that can also run PHP with automatic HTTPS can look like this:

yourdomain.com, www.yourdomain.com

root * /path/to/webfolder
encode gzip
php_fastcgi unix//path/to/php.sock
file_server

The converted equivalant (caddy adapt --config /path/to/Caddyfile --pretty) in JSON looks like this:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":443"
          ],
          "routes": [{
            "match": [{
              "host": [
                "yourdomain.com",
                "www.yourdomain.com"
              ]
            }],
            "handle": [{
              "handler": "subroute",
              "routes": [{
                "handle": [{
                    "handler": "vars",
                    "root": "/path/to/webfolder"
                  },
                  {
                    "handler": "subroute",
                    "routes": [{
                        "handle": [{
                          "encodings": {
                            "gzip": {}
                          },
                          "handler": "encode"
                        }]
                      },
                      {
                        "handle": [{
                          "handler": "rewrite",
                          "rehandle": true,
                          "uri": "{http.matchers.file.relative}{http.request.uri.query_string}"
                        }],
                        "match": [{
                          "file": {
                            "try_files": [
                              "{http.request.uri.path}",
                              "index.php"
                            ]
                          }
                        }]
                      },
                      {
                        "handle": [{
                          "handler": "reverse_proxy",
                          "transport": {
                            "protocol": "fastcgi",
                            "split_path": ".php"
                          },
                          "upstreams": [{
                            "dial": "unix//path/to/php.sock"
                          }]
                        }],
                        "match": [{
                          "path": [
                            "*.php"
                          ]
                        }]
                      },
                      {
                        "handle": [{
                          "handler": "file_server",
                          "hide": [
                            "Caddyfile"
                          ]
                        }]
                      }
                    ]
                  }
                ]
              }]
            }]
          }]
        }
      }
    },
    "tls": {
      "automation": {},
      "session_tickets": {}
    }
  }
}

Reverse proxy to HTTP golang servers on port 8080 with automatic TLS and health checks

Configuring Caddy as a reverse proxy to HTTP golang servers on port 8080 with automatic TLS and health checks can look like this:

yourdomain.com, www.yourdomain.com

reverse_proxy / {
	to 192.101.153.159:8080 192.118.109.166:8080
	health_path /health
	health_status 200
	transport http {
		read_buffer 4096
	}
}

The converted equivalant (caddy adapt --config /path/to/Caddyfile --pretty) in JSON looks like this:

{
   "apps":{
      "http":{
         "servers":{
            "srv0":{
               "listen":[
                  ":443"
               ],
               "routes":[
                  {
                     "match":[
                        {
                           "host":[
                              "yourdomain.com",
                              "www.yourdomain.com"
                           ]
                        }
                     ],
                     "handle":[
                        {
                           "handler":"subroute",
                           "routes":[
                              {
                                 "handle":[
                                    {
                                       "handler":"reverse_proxy",
                                       "health_checks":{
                                          "active":{
                                             "expect_status":2,
                                             "path":"/health"
                                          }
                                       },
                                       "transport":{
                                          "protocol":"http",
                                          "read_buffer_size":4096
                                       },
                                       "upstreams":[
                                          {
                                             "dial":"192.101.153.159:8080"
                                          },
                                          {
                                             "dial":"192.118.109.166:8080"
                                          }
                                       ]
                                    }
                                 ],
                                 "match":[
                                    {
                                       "path":[
                                          "/"
                                       ]
                                    }
                                 ]
                              }
                           ]
                        }
                     ]
                  }
               ]
            }
         }
      }
   }
}

Note: do not forget to set up health handlers for the upstream webservers returning an HTTP 200 response or Caddy 2 will fail the health check and deem the node unreachable.

You can’t perform that action at this time.