Enabling HTTPS for Local Development with Caddy

When developing locally, you might want to serve your site over HTTPS. While Caddy automatically provides HTTPS for public domains, serving HTTPS on localhost directly is not supported. However, you can achieve this by using a custom domain name.

Basic Configuration

To start, you can use a simple Caddyfile configuration to proxy requests to your local application. Here’s an example of a basic setup:

localhost:2020 {
  bind {$ADDRESS}
  reverse_proxy http://192.168.100.82:9000 {
    transparent
  }
}

This configuration allows you to access your application running on 192.168.100.82:9000 through localhost:2020. However, this setup does not enable HTTPS.

Using a Custom Domain

To enable HTTPS, you need to use a custom domain name. Here’s how you can configure Caddy to use a local IP address with a custom domain:

192.168.100.26 {
  bind {$ADDRESS}
  reverse_proxy http://192.168.100.82:9000 {
    transparent
  }
}

While this works for local development, you still won’t have HTTPS unless you use a domain that resolves correctly.

Setting Up a Domain

If you want to use a domain like www.mycaddytest.com, ensure that the domain is correctly set up in your DNS provider to point to your server's public IP address. If the domain does not exist or is not properly configured, you will encounter errors like:

Activating privacy features... failed to get certificate: acme: Error 400 - urn:acme:error:connection - DNS problem: NXDOMAIN looking up A for www.mycaddytest.com

This error indicates that the domain cannot be resolved, which is necessary for Caddy to obtain a valid TLS certificate.

Local HTTPS with Self-Signed Certificates

If you just want to test HTTPS locally without a public domain, you can run Caddy with a self-signed certificate. Here’s an example configuration:

localhost {
  reverse_proxy http://192.168.100.82:9000 {
    transparent
  }
  tls internal
}

This will serve your site over HTTPS using a self-signed certificate that is trusted locally. You may need to install Caddy's root certificate in your local trust store to avoid security warnings in your browser.

Conclusion

Using Caddy for local development with HTTPS can be straightforward if you understand how to configure domains and certificates. For public domains, ensure your DNS settings are correct, and for local testing, leverage Caddy's internal TLS capabilities.