Skip to content
local-cert
Browse the documentation

Nginx

In most setups Nginx doesn't request certificates itself: an ACME client runs alongside it, writes the files and reloads Nginx. certbot is the usual choice.

ValueWhere to find itIn this guide
ACME directoryfixed for each instancehttps://certs.example.com/acme/directory
Key ID (kid)Keys & access → EAB credentialseab_XXXXXXXXXXXXXXXX
HMAC keyshown only once, at creationCLE_HMAC

1. Get the certificate

Nginx holds port 80: use webroot mode with an empty folder. It doesn't need to be served by Nginx, since local-cert never reads from it.

certbot
mkdir -p /var/lib/letsencrypt/webroot
certbot certonly --webroot -w /var/lib/letsencrypt/webroot -d nas.home.example.com \
  --server https://certs.example.com/acme/directory \
  --eab-kid eab_XXXXXXXXXXXXXXXX --eab-hmac-key CLE_HMAC \
  --email ops@example.com --agree-tos --no-eff-email \
  --deploy-hook "systemctl reload nginx"

2. Wire Nginx to it

/etc/nginx/conf.d/nas.home.example.com.conf
server {
    listen 443 ssl;
    http2 on;
    server_name nas.home.example.com;

    ssl_certificate     /etc/letsencrypt/live/nas.home.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/nas.home.example.com/privkey.pem;

    location / {
        proxy_pass http://192.168.1.50:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

server {
    listen 80;
    server_name nas.home.example.com;
    return 301 https://$host$request_uri;
}
check and reload
nginx -t && systemctl reload nginx

3. Renewal

The certbot package installs a timer that runs certbot renew twice a day. The ACME server, the EAB and the --deploy-hook are remembered: Nginx is reloaded on every renewal. Dry run:

certbot
certbot renew --dry-run

Variants

  • acme.sh instead of certbot: same principle, see certbot, acme.sh, lego.
  • Nginx in a container: run certbot on the host (or in its own container) with /etc/letsencrypt mounted in both, and replace the hook with docker exec nginx nginx -s reload.