Skip to content
nginx-lePublic

About

Nginx with automatic let's encrypt (docker image)

Topics

Resources

Stars

686 stars

Watchers

17 watching

Forks

Repository files navigation

NGINX-LE - Nginx web and proxy with automatic let's encrypt build

Simple nginx image (alpine based) with integrated Let's Encrypt support.

How to use

  • get docker-compose.yml and change things:

    • set timezone to your local, for example TZ=UTC. For more timezone values check /usr/share/zoneinfo directory
    • set LETSENCRYPT=true if you want an automatic certificate install and renewal
    • LE_EMAIL should be your email and LE_FQDN for domain
    • for multiple FQDNs you can pass comma-separated list, like LE_FQDN=aaa.example.com,bbb.example.com
    • alternatively set LETSENCRYPT to false and pass your own cert in SSL_CERT, key in SSL_KEY and SSL_CHAIN_CERT
    • LE_ADDITIONAL_OPTIONS can be set to anything you want to append to certbot, for example LE_ADDITIONAL_OPTIONS=--preferred-chain "ISRG Root X1" --debug.
    • use provided etc/service-example.conf to make your own etc/service.conf. Keep ssl directives as is:
      ssl_certificate SSL_CERT;
      ssl_certificate_key SSL_KEY;
      ssl_trusted_certificate SSL_CHAIN_CERT;
    • by default nginx drops the global headers from conf/nginx.conf (X-Frame-Options, X-Content-Type-Options, X-XSS-Protection) at any level with its own add_header. The example keeps them with add_header_inherit merge; in its server block (nginx 1.29.3+). Merging also applies to nested blocks, so a header set at two levels is sent twice
  • make sure volumes in docker-compose.yml changed to your service config

  • you can map multiple custom config files to in compose using service*.conf filename pattern, see service2.conf in docker-compose.yml file for reference

    Alternatively, mount directory with *.conf files into /etc/nginx/conf.d-le directory inside the container to have them all copied at once.

  • stream*.conf files are picked up into /etc/nginx/stream.d/ directory and included into stream section of the Nginx configuration, see stream2.conf in docker-compose.yml file for reference.

    Alternatively, mount directory with *.conf files into /etc/nginx/stream.conf.d-le directory inside the container to have them all copied at once.

  • pull image - docker-compose pull

  • if you don't want a pre-built image, make you own. docker-compose build will do it

  • start it docker-compose up

Local smoke test

To check a build without involving Let's Encrypt, start the image with LETSENCRYPT=false and ask it for the built-in http to https redirect:

docker build -t nginx-le .
docker run -d --name nginx-le-smoke -e LETSENCRYPT=false -p 127.0.0.1:8080:80 nginx-le

# the first start generates dh parameters and takes a while
until curl -s -o /dev/null http://127.0.0.1:8080/; do sleep 2; done
curl -sI http://127.0.0.1:8080/ | head -1   # HTTP/1.1 301 Moved Permanently

docker rm -f nginx-le-smoke

Configuration files variables replacement

On start of the container all following text matches in custom configuration files you mounted will be replaced, variable with dollar sign ($, like $LE_FQDN) will be taken from environment, please see next table for their list.

Matching pattern Value nginx usage Description
SSL_CERT /etc/nginx/ssl/$SSL_CERT ssl_certificate Public SSL certificate, sent to client
SSL_KEY /etc/nginx/ssl/$SSL_KEY ssl_certificate_key SSL private key, not sent to client
SSL_CHAIN_CERT /etc/nginx/ssl/$SSL_CHAIN_CERT ssl_trusted_certificate Trusted SSL certificates, not sent to client
LE_FQDN $LE_FQDN, commas replaced by spaces in server_name server_name List of domains, useful for configuration with single server block

Environment variables list

Variable Default value Description
SSL_CERT le-crt.pem certbot fullchain.pem new filename
SSL_KEY le-key.pem certbot privkey.pem new filename
SSL_CHAIN_CERT le-chain-crt.pem certbot chain.pem new filename
LETSENCRYPT false Enables Let's Encrypt certificate retrieval and renewal
LE_FQDN comma-separated list of domains for Let's Encrypt certificate, required if LETSENCRYPT is true
LE_EMAIL comma-separated list of emails for Let's Encrypt certificate, required if LETSENCRYPT is true
LE_ADDITIONAL_OPTIONS Additional options to be appended to certbot command
TZ Timezone, if set will be written to container's /etc/timezone

envsubst templates

If you need to use any other environment variables you could mount your configuration as /etc/nginx/templates/yourservice.conf.template and envsubsts would be executed on that file. More info could be found here

Some implementation details

Important: provided nginx.conf handles http->https redirect automatically, no need to add it into your custom service.conf. In case if you need a custom server on http (:80) port, make sure you handle /.well-known/ path needed with root set for LE challenge: location /.well-known/ {root /usr/share/nginx/html;}

  • image uses alpine's certbot package.
  • OCSP stapling is not enabled, Let's Encrypt retired its OCSP responders and their certificates carry no responder URL. If you bring your own certificate from a CA that still publishes one, or one with the Must-Staple extension, enable ssl_stapling and ssl_stapling_verify in your own server block.
  • request size limits are the nginx defaults, client_max_body_size 1m and large_client_header_buffers 4 8k. Older images capped both at 4k. To change them for all sites, including the built-in HTTP redirect server, set them in a service config outside any server block. client_max_body_size also works inside one site's server block. large_client_header_buffers there is reliable only for HTTPS sites, because nginx reads a plain HTTP request line with the default server's value. To keep the old limits, put client_max_body_size 4k; and large_client_header_buffers 2 4k; outside any server block.
  • TLS key exchange groups are the defaults of the bundled OpenSSL, which in the current image include the post-quantum hybrid X25519MLKEM768, X25519, P-256 and P-384. Older images pinned ECDHE and TLS 1.3 key exchange to P-384 (ssl_ecdh_curve secp384r1), so TLS 1.3 clients offering only X25519 were refused. To set the groups for all sites, put ssl_ecdh_curve in a service config outside any server block.
  • script/entrypoint.sh requests LE certificate and will refresh every 10 days in case if certificate is close to expiration (30day), misses one of the LE_FQDN domains, doesn't match the installed key or lost its chain file. When certbot fails to get a certificate, for example because DNS doesn't point to the host yet or port 80 isn't reachable, it tries again every hour. After about 50 days of failures in a row Let's Encrypt pauses issuance for the domain, and the certbot error in the log links to the page that lifts the pause
  • script/le.sh gets SSL
  • nginx-le on docker-hub
  • A+ overall rating on ssllabs

ssllabs

Alternatives

  • Reproxy simple edge HTTP(s) server / reverse proxy supporting various providers.
  • Træfik HTTP reverse proxy and load balancer. Supports Let's Encrypt directly.
  • Caddy supports Let's Encrypt directly.
  • leproxy small and nice (stand alone) https reverse proxy with automatic Letsencrypt
  • bunch of others

Examples

  • Reverse proxy for WebRTC solutions, where you need multiple ports on one domain to reach different services behind your nginx-le container.

Manual certificate renewal (*.example.com, DNS challenge)

wildcard certificate renewal

In your docker-compose.yml disable automatic Let's Encrypt certificate creation/renewal.

    environment:
      - LETSENCRYPT=false
# after starting nginx-le connect to it
docker exec -it nginx sh

# change `*.example.com` to your domain name
certbot certonly \
    --manual \
    --manual-public-ip-logging-ok \
    --preferred-challenges=dns \
    --email "${LE_EMAIL}" \
    --agree-tos \
    -d "*.example.com"

# it will ask you to create/update TXT DNS record
# depending on your DNS provider it can take some time
# you can check if DNS is already updated using dig utility
dig txt _acme-challenge.example.com

# copy certificates for nginx-le to use them
cp -fv /etc/letsencrypt/live/example.com/privkey.pem /etc/nginx/ssl/le-key.pem
cp -fv /etc/letsencrypt/live/example.com/fullchain.pem /etc/nginx/ssl/le-crt.pem
cp -fv /etc/letsencrypt/live/example.com/chain.pem /etc/nginx/ssl/le-chain-crt.pem

# use the same procedure for renewal

About

Nginx with automatic let's encrypt (docker image)

Topics

Resources

Stars

686 stars

Watchers

17 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages