Use tls internal for local development but the cert_file in production

1. The problem I’m having:

I’d like to configure the Caddyfile to use tls internal if the environment variable ENV is development. Otherwise, it should use the default one.

2. Error messages and/or full log output:

I didn’t get any error messages.

3. Caddy version:

v2.6.4

4. How I installed and ran Caddy:

I used in a docker container caddy:latest.

a. System environment:

Linux f8d3ec657a2a 5.19.0-42-generic #43-Ubuntu SMP PREEMPT_DYNAMIC Tue Apr 18 18:21:28 UTC 2023 x86_64 Linux

b. Command:

c. Service/unit/compose file:

d. My complete Caddy config:

*.pronus.io {
    # the default configuration
    tls {
        dns digitalocean {env.DIGITAL_OCEAN_TOKEN}
    }

    # the configuration I'm looking for
    # if $ENV == "development"
    #     tls internal
    # else
    #     tls {
    #         dns digitalocean {env.DIGITAL_OCEAN_TOKEN}
    #     }

    @compress_exts {
        not path *.gif *.jpg *.png
    }

    encode @compress_exts gzip zstd

    @codelab host codelab.pronus.io
    handle @codelab {
        handle_path /api* {
            reverse_proxy h2c://codelab:5000
        }

        handle {
            root * /static_data/dist
            try_files {path} /index.html
            file_server
        }
    }

    @transfer host transfer.pronus.io
    handle @transfer {
        reverse_proxy h2c://transfer:5000
    }

    @blog host blog.pronus.io
    handle @blog {
        root * /static_data/blog
        file_server
    }

    # Fallback for otherwise unhandled domains
    handle {
        redir https://blog.pronus.io{uri}
    }
}

5. Links to relevant resources:

Just make two config files, one named Caddyfile.dev and the other Caddyfile.prod and use one or the other depending on the environment.

Or you can use snippets, something like this:

(tls-dev) {
	tls internal
}

(tls-prod) {
	tls {
		dns digitalocean {env.DIGITAL_OCEAN_TOKEN}
	}
}

*.pronus.io {
	import tls-{$ENV}
}
1 Like

It looks great! Thank you very much!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.