Project status
Use case
A docker based proxy server that is easy to configure. The image bundles an nginx web server with a certbot service that retrieves and renews Let's Encrypt certificates for a configurable list of domains. Optionally the certificates are uploaded to Cloudflare, so that Cloudflare's edge serves the same certificates and keeps them up to date.
Content
Features
- Nginx reverse proxy based on the Arch Linux containerbase image (runtime user handling, environment files and encrypted configuration included).
- Plain nginx configuration: every "*.conf" file in "/etc/nginx/conf.d" is included into the "http" block.
- Default server answering ACME challenges and redirecting every other HTTP request to HTTPS.
- Hardened TLS defaults (TLS 1.2 and 1.3, modern ciphers, HSTS, OCSP stapling) as reusable include files.
- Certificates are retrieved on startup and checked daily for renewal, any number of them, each covering multiple domains.
- A certificate is retrieved again when its domain list changes.
- Optional synchronization of every certificate into a Cloudflare zone as custom certificate.
- Ready to use docker compose, podman (kubernetes) and systemd service files.
Installation
Pull the prebuilt image (built weekly for "x86-64" and "arm-64"):
docker pull ghcr.io/thaibault/certbot-proxy:latest
The service files and the configuration skeleton live in this repository, so clone it to the host:
git clone https://github.com/thaibault/certbot-proxy.git
cd certbot-proxy
To build the image yourself run:
docker pull ghcr.io/thaibault/containerbase:latest
docker buildx build --no-cache --tag ghcr.io/thaibault/certbot-proxy:latest .
Usage
Folder structure
The container expects three mounts (see "service/docker/mounts.yaml"):
- "certificates/" → "/application/certificates": Retrieved certificates, certbot state, ACME challenges and log.
- "etc/containerBase/" → "/etc/containerBase": "environment.sh" holding the configuration described below.
- "etc/nginx/" → "/etc/nginx/conf.d": Your nginx server configurations and helper includes.
Start
Locally (HTTP on port 8080, HTTPS on port 4443, local scripts mounted for testing and no delay before the first certificate check):
yarn start
which is a shortcut for:
docker compose \
--file service/docker/base.yaml \
--file service/docker/mounts.yaml \
--file service/docker/local.yaml \
up
In production use "service/docker/production.yaml" instead of "service/docker/local.yaml". It binds the ports 80 and 443, restarts on failure, limits log sizes and attaches the proxy to an external docker network named "certbot-proxy". Attach your application containers to that network to reach them by their container name from nginx:
docker network create certbot-proxy
docker compose \
--file service/docker/base.yaml \
--file service/docker/mounts.yaml \
--file service/docker/production.yaml \
--project-name certbot-proxy \
up
With podman use the kubernetes manifests:
podman play kube service/kubernetes/production.yaml
The environment variable "IMAGE_TAG" selects the image tag when using docker compose ("latest" by default).
Systemd
"service/systemd" contains units to run the proxy on a host:
- "certbot-proxy.service" creates the "certbot-proxy" network and runs the production compose setup from "/srv/http/service/certbot-proxy" as user "service".
- "reload-certbot-proxy.service" reloads the nginx configuration (and with it all certificate files) of the running container.
- "reload-certbot-proxy.timer" triggers "reload-certbot-proxy.service" five minutes after boot and once a week afterwards.
On Arch Linux the included "PKGBUILD" builds a package via "makepkg --install". It installs the compose files and the configuration skeleton into "/srv/http/service/certbot-proxy" and the units into systemd's unit folder. The user "service" has to exist and be allowed to use docker:
useradd --groups docker --system service
systemctl enable --now certbot-proxy.service reload-certbot-proxy.timer
Nginx configuration
All files matching "/etc/nginx/conf.d/*.conf" are included into the main "http" block. The image itself only defines global settings (temporary paths, gzip, websocket aware proxy headers and "resolver 8.8.8.8"). The included files can be changed at build time via "CERTBOT_PROXY_APPLICATION_SPECIFIC_NGINX_CONFIGURATION_FILE_PATH".
Default server
"etc/nginx/00_default.conf" defines the log formats and a default server on port 80 that:
- serves "/.well-known/acme-challenge/" from "/application/certificates/acme-challenge/" (needed for certificate retrievals and renewals) and
- redirects every other request permanently to HTTPS.
Helper includes
- "helper/ssl.include" contains the TLS protocol, cipher, session, HSTS and OCSP stapling settings.
- "helper/certificate.include" includes "ssl.include" and points nginx to the files of a certificate named "web". Nginx does not support variables in these directives, so for certificates with other names copy the file and replace "web" accordingly.
The include also expects a Diffie-Hellman parameter file, which has to be created once:
mkdir --parents certificates/web
openssl dhparam -out certificates/web/dhparam.pem 4096
A typical application server configuration (e.g. "etc/nginx/application.conf") looks like this:
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name example.com www.example.com;
include conf.d/helper/certificate.include;
location / {
proxy_pass http://application:8080;
}
}
Certificates
Configuration
Certificates are configured in "etc/containerBase/environment.sh", which is sourced on container start. The lists are Bash arrays matched by their index:
# Name of each certificate. Files will be stored under
# "certificates/<name>/letsEncrypt/configuration/live/<name>/".
CERTBOT_PROXY_CERTIFICATES=(web web.staging)
# Space separated domains each certificate should cover.
CERTBOT_PROXY_CERTIFICATE_DOMAINS=(
'example.com www.example.com'
'staging.example.com www.staging.example.com'
)
# Let's Encrypt account e-mail address per certificate. An empty or missing
# entry reuses the address of the previous certificate.
CERTBOT_PROXY_CERTIFICATE_EMAIL_ADDRESSES=([email protected])
# Delay before the first renewal check after startup (any "sleep" duration).
CERTBOT_PROXY_CERTIFICATES_START_UPDATE_DELAY=50m
A single certificate can also be configured via plain environment variables (e.g. in a compose file), since a scalar acts like a one element array. Without "CERTBOT_PROXY_CERTIFICATES" no certificates are managed and only nginx is started.
Lifecycle
- Startup: Every certificate that has not been retrieved yet, or whose domain list differs from the one it was retrieved for (stored in "certificates/<name>/domains.txt"), is retrieved via certbot's own web server before nginx starts.
- Start delay: The certificate service waits "CERTBOT_PROXY_CERTIFICATES_START_UPDATE_DELAY" before its first check, so that frequent container restarts do not run into Let's Encrypt rate limits.
- Daily checks: Every 24 hours each certificate is renewed through the running nginx when it is close to expiry. Certificates with a changed domain list are retrieved again.
- Reload: Nginx is reloaded right after each renewal or retrieval, so new certificates are served immediately. Additionally it is reloaded every seventh check.
All certificate activity is logged to "certificates/log.txt" (configurable via "CERTIFICATION_SERVICE_LOG"). Certbot's own logs are written to "/tmp/<name>/letsEncryptLog" inside the container.
Cloudflare synchronization
Each certificate can additionally be uploaded to a Cloudflare zone as custom certificate. The proxy keeps it up to date: the first upload creates the custom certificate, every renewal replaces it, and if it has been deleted on Cloudflare a new one is created.
# Zone id per certificate (same order as "CERTBOT_PROXY_CERTIFICATES"). An
# empty entry disables the synchronization for that certificate.
CERTBOT_PROXY_CERTIFICATE_CLOUDFLARE_ZONE_IDS=(023e105f4ecef8ad9ca31a8372d0c353 '')
# API token with the permission "Zone - SSL and Certificates - Edit".
CERTBOT_PROXY_CLOUDFLARE_API_TOKEN=YOUR_TOKEN
# Optional per certificate tokens overwriting
# "CERTBOT_PROXY_CLOUDFLARE_API_TOKEN".
CERTBOT_PROXY_CERTIFICATE_CLOUDFLARE_API_TOKENS=('' '')
# Optional: "ubiquitous" (default), "optimal" or "force".
CERTBOT_PROXY_CLOUDFLARE_BUNDLE_METHOD=ubiquitous
# Optional: "sni_custom" (default) or "legacy_custom".
CERTBOT_PROXY_CLOUDFLARE_CERTIFICATE_TYPE=sni_custom
Synchronization runs on startup (so existing certificates are uploaded as soon as a zone is configured) and after every daily check. An upload only happens if the certificate or key changed since the last successful upload. The Cloudflare certificate id and a fingerprint of the uploaded files are stored in "certificates/<name>/cloudflareCertificateID.txt" and "certificates/<name>/cloudflareCertificateFingerprint.txt". Delete them to force a new upload. A failing upload is logged but neither prevents nginx from starting nor stops the certificate service.
- Custom certificates require a Cloudflare Business or Enterprise plan, or the Advanced Certificate Manager add-on for "sni_custom".
- Let's Encrypt validates domains via HTTP. Cloudflare passes "/.well-known/acme-challenge/" requests through to the origin even with proxying and "Always Use HTTPS" enabled, so renewals keep working.
- Store the API token in an encrypted environment file (see containerbase) rather than in plain text.
Environment variables
- "CERTBOT_PROXY_CERTIFICATES": Certificate names (array).
- "CERTBOT_PROXY_CERTIFICATE_DOMAINS": Space separated domains per certificate (array).
- "CERTBOT_PROXY_CERTIFICATE_EMAIL_ADDRESSES": Let's Encrypt account e-mail address per certificate (array).
- "CERTBOT_PROXY_CERTIFICATES_START_UPDATE_DELAY": Delay before the first renewal check, "50m" by default.
- "CERTBOT_PROXY_CERTIFICATE_CLOUDFLARE_ZONE_IDS": Cloudflare zone id per certificate (array).
- "CERTBOT_PROXY_CERTIFICATE_CLOUDFLARE_API_TOKENS": Cloudflare API token per certificate (array, optional).
- "CERTBOT_PROXY_CLOUDFLARE_API_TOKEN": Default Cloudflare API token.
- "CERTBOT_PROXY_CLOUDFLARE_API_URL": Cloudflare API base URL, "https://api.cloudflare.com/client/v4" by default.
- "CERTBOT_PROXY_CLOUDFLARE_BUNDLE_METHOD": Chain bundling of uploaded certificates, "ubiquitous" by default.
- "CERTBOT_PROXY_CLOUDFLARE_CERTIFICATE_TYPE": Type of uploaded custom certificates, "sni_custom" by default.
- "CERTBOT_PROXY_APPLICATION_SPECIFIC_NGINX_CONFIGURATION_FILE_PATH": Nginx configuration files to include at build time, "/etc/nginx/conf.d/*.conf" by default.
- "CERTIFICATION_SERVICE_LOG": Log file of the certificate service, "/application/certificates/log.txt" by default.
- "COMMAND": Command started after initialization, "nginx" by default.
Variables of the containerbase image are supported as well, e.g. to change the runtime user or to decrypt encrypted configuration files ("DECRYPT", "ENCRYPTED_PATHS", "DECRYPTED_PATHS").
Container commands
The following commands handle certificates manually inside the container (e.g. via docker exec certbot-proxy <command>). "<path>" is the certificate folder, e.g. "/application/certificates/web/" (the trailing slash is required):
- "retrieve-certificate [--initialize] <name> <path> '<domains>' <e-mail>": Retrieves a certificate. "--initialize" uses certbot's own web server while nginx is not running.
- "update-certificate <name> <path> '<domains>' <e-mail>": Renews a certificate if it is close to expiry.
- "synchronize-cloudflare-certificate <name> <path> <zone-id>": Uploads a certificate to Cloudflare (needs "CERTBOT_PROXY_CLOUDFLARE_API_TOKEN").
- "initialize-certificates": Retrieves all configured certificates which are missing.
- "certificate-service": Runs the periodic renewal loop.
- "reload-nginx": Reloads the nginx configuration and with it all certificates.
Development
yarn install
# Lint all shell scripts via shellcheck.
yarn lint
# Run the scripts against recorded fake commands (needs "python3").
yarn test
# Start locally with the scripts of this working copy mounted.
yarn start
For testing against Let's Encrypt's staging environment add "--test-cert" to the certbot call in "scripts/retrieve-certificate.sh".
About this website
Provider ofhttps://tsickert.com/certbot-proxy:
Torben Sickert
Waldstraße 29
79194 Gundelfingen i.Br.
Phone: +49 (0) 176 / 10 248 185
Email: [email protected]
Website: https://tsickert.com
Disclaimer
Liability for content
The content of our pages has been prepared with the utmost care. However, we cannot guarantee the accuracy, completeness or timeliness of the content. As a service provider, we are responsible for our own content on these pages under general law. However, we are not obliged to monitor transmitted or stored third-party information or investigate circumstances that indicate illegal activity. Obligations to remove or block the use of information under general law remain unaffected. Liability in this respect is only possible from the time we become aware of a specific infringement. Upon becoming aware of any such legal violations, we will remove this content immediately.
Liability for links
Our website contains links to external third-party websites whose content is beyond our control. We therefore cannot accept any responsibility for such third-party content. The respective provider or operator of each linked site is always responsible for its content. The linked sites were checked for possible legal violations when the links were created. No illegal content was apparent at that time. However, continuous monitoring of linked sites is not reasonable without specific evidence of a legal violation. If we become aware of any legal violations, we will remove the relevant links immediately.
Copyright
Content and works on these pages created by the site operators are subject to German copyright law. Reproduction, adaptation, distribution and any use beyond the scope of copyright law require the written consent of the respective author or creator. Downloads and copies of this site are permitted for private, non-commercial use only. Where content on this site was not created by the operator, third-party copyrights are respected. In particular, third-party content is identified as such. If you become aware of a copyright infringement, please notify us. Upon becoming aware of any legal violations, we will remove the relevant content immediately.
Privacy Policy
You can generally use our website without providing personal data. Where personal data (such as names, addresses or email addresses) is collected on our pages, this is, wherever possible, always done on a voluntary basis. This data will not be passed on to third parties without your express consent. Please note that data transmission over the internet (for example, when communicating by email) may have security vulnerabilities. It is not possible to completely protect data against access by third parties. We expressly object to the use by third parties of contact details published to meet legal notice requirements for sending unsolicited advertising or information. The site operators expressly reserve the right to take legal action if unsolicited advertising, such as spam emails, is sent.
Content Delivery Network (Cloudflare)
We use Cloudflare (Cloudflare, Inc., 101 Townsend St., San Francisco, CA 94107, USA) to protect and speed up our website. This constitutes a legitimate interest within the meaning of Art. 6(1)(f) GDPR. Cloudflare processes log data (including IP addresses) and sets technically necessary cookies to defend against malicious bot traffic. We have concluded a Data Processing Addendum (DPA) with Cloudflare based on the EU Standard Contractual Clauses. Cloudflare is certified under the EU-U.S. Data Privacy Framework.
Privacy Statement for a possible use of Google Analytics
This website may use Google Analytics 4, a web analytics service provided by Google Ireland Limited ("Google", Gordon House, Barrow Street, Dublin 4, Ireland). If it is used, data is processed solely on the basis of your express consent (Art. 6(1)(a) GDPR). Google Analytics uses cookies that enable analysis of how you use the website. Information generated by these cookies about your use of this website is transmitted to and stored on a Google server; transfer of data to third countries (such as the United States) cannot be ruled out. IP anonymization is enabled by default on this website, so Google truncates your IP address within a member state of the European Union or another state party to the Agreement on the European Economic Area before transmission. You may withdraw your consent at any time with effect for the future through the cookie settings (our consent tool), or prevent data collection by using the official Google browser add-on.
Copyright Statement
Unless noted otherwise, all artwork on this website is protected property of the website author. Any use for commercial purpose, reproduction and publication requires the author's written permission. Commercial work created in cooperation or bound by contract with third parties are specifically marked. Respective copyrights apply.