When developing web services, you may sometimes need to test how things work with a real domain. In most cases, you can add domains to the /etc/hosts file; for example, it might look like this:
127.0.0.1 site.localhost
127.0.0.1 admin.localhost
127.0.0.1 db.localhost
You can also use one of the actual domains that has its DNS A-record set to 127.0.0.1, for example:
- lvh.me - see here for instructions on how to use it.
- localtest.me - learn more here.
- 127.0.0.1.nip.io - a particularly useful service that also provides access to other IP addresses not routable on the public internet; find out more here.
But what if you need your service to use a secure connection on localhost - that is, to be accessible via HTTPS? One option is to use self-signed certificates and add them to the system’s SSL/TLS certificate store (on Debian, for instance, you would place the certificate in the /usr/local/share/ca-certificates/ directory and update the system certificate store by running the command sudo update-ca-certificates).
Another option is to use your own domain. For example, if you own the domain example.com, you can add an A record pointing to the IP address 127.0.0.1 for the subdomain localhost.example.com, and then obtain a 90-day SSL certificate from Let’s Encrypt using DNS-based validation.
To do this, you need to follow these steps:
- Log in to the admin control panel for your domain (e.g., example.com) and add an A-record:
Subdomain: localhost
IP: 127.0.0.1
- Create a folder on your workstation to be mounted into the Docker container (this folder will store the certificates), and use the ACME client “lego” to manually request a certificate via DNS.
mkdir lego-certs
docker run -it --rm -v "$(pwd)/lego-certs:/.lego" goacme/lego run \
-d 'localhost.example.com' \
--key-type EC256 \
--dns manual \
--accept-tos
You can review the available flags here.
- Once these lines appear in the logs:
2026-11-16T10:08:56.499002562Z INFO Obtaining bundled SAN certificate. domains=localhost.example.com
2026-11-16T10:08:57.141449703Z INFO Use solver. domain=localhost.example.com type=dns-01
2026-11-16T10:08:57.141510958Z INFO dns01: preparing to solve the challenge. domain=localhost.example.com
lego: Please create the following TXT record in your example.com. zone:
_acme-challenge.localhost.example.com. 120 IN TXT "z0zgBEhgp8EPQpjXWTLytErUfd6NiYs2hP___1N8g6w"
lego: Press 'Enter' when you are done
In the DNS admin panel you need to create a TXT record with the following content:
Subdomain: _acme-challenge.localhost
TXT: z0zgBEhgp8EPQpjXWTLytErUfd6NiYs2hP___1N8g6w
Wait about 3–5 minutes for it to update; you can check as follows:
dig TXT _acme-challenge.localhost.example.com +short
And press Enter.
- If everything has been done correctly and no further verification keys need to be added, the output will be as follows:
2026-11-16T10:16:47.515048470Z INFO dns01: trying to solve the challenge. domain=localhost.example.com
2026-11-16T10:16:47.560410651Z INFO dns01: waiting for record propagation timeout=1m0s interval=2s domain=localhost.example.com
2026-11-16T10:16:57.424361762Z INFO The server validated our request. domain=localhost.example.com
2026-11-16T10:16:57.424414984Z INFO dns01: cleaning DNS-01 challenge. domain=localhost.example.com
lego: You can now remove this TXT record from your example.com. zone:
_acme-challenge.localhost.example.com. 120 IN TXT "..."
2026-11-16T10:16:57.428225575Z INFO Validations succeeded; requesting certificates. domains=localhost.example.com
2026-11-16T10:16:59.981495942Z INFO Server responded with a certificate. domains=localhost.example.com
2026-11-16T10:16:59.981887712Z INFO Writing file. filepath=/.lego/certificates/localhost.example.com.crt
2026-11-16T10:16:59.982131313Z INFO Writing file. filepath=/.lego/certificates/localhost.example.com.issuer.crt
2026-11-16T10:16:59.982350041Z INFO Writing file. filepath=/.lego/certificates/localhost.example.com.key
2026-11-16T10:16:59.982663266Z INFO Writing file. filepath=/.lego/certificates/localhost.example.com.json
After this, the TXT record that was added (in step 3) can be removed from the DNS.
The lego-certs/certificates folder will now contain the following files:
localhost.example.com.crt- the full certificate chain (from the leaf certificate up to the root).localhost.example.com.issuer.crt- the certificate chain containing only the intermediate and root certificates (excluding the leaf certificate).localhost.example.com.json- a file containing information about where and how the certificate was signed.localhost.example.com.key- the private key.
To display information about an issued certificate, you need to execute the following command:
openssl x509 -in localhost.example.com.crt -text -noout
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
05:32:dd:d1:c0:d6:a5:a2:76:28:ae:d2:2d:ca:c9:fb:ec:77
Signature Algorithm: ecdsa-with-SHA384
Issuer: C = US, O = Let's Encrypt, CN = YE2
Validity
Not Before: Nov 16 09:18:45 2026 GMT
Not After : Feb 14 09:18:44 2027 GMT
Subject: CN = localhost.example.com
...
To view information about all intermediate and root certificates, you need to run the following command:
openssl storeutl -text -noout localhost.example.com.crt
If you do not need the certificate chain, you can extract and save only your own certificate from the chain, as follows:
openssl x509 -in localhost.example.com.crt -out localhost.example.com.single.crt
chmod 600 localhost.example.com.single.crt
openssl x509 -in localhost.example.com.single.crt -text -noout
Now, all that remains is to copy the certificate and key to another location (ensuring that the owner is set to root and the correct permissions are applied) and add them to your Nginx configuration. You can do this as follows:
mkdir -p /etc/nginx/ssl
cp lego-certs/certificates/localhost.example.com.crt /etc/nginx/ssl/
cp lego-certs/certificates/localhost.example.com.key /etc/nginx/ssl/
chown root:root /etc/nginx/ssl/localhost.example.com.crt
chown root:root /etc/nginx/ssl/localhost.example.com.key
chmod 644 /etc/nginx/ssl/localhost.example.com.crt
chmod 600 /etc/nginx/ssl/localhost.example.com.key
Create an Nginx configuration file (/etc/nginx/conf.d/localhost.example.conf):
server {
listen 80;
listen [::]:80;
server_name localhost.example.com;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name localhost.example.com;
ssl_certificate /etc/nginx/ssl/localhost.example.com.crt;
ssl_certificate_key /etc/nginx/ssl/localhost.example.com.key;
location / {
charset utf-8;
default_type text/plain;
return 200 "Hello world";
}
}
Check the configuration and update the settings
nginx -t && nginx -s reload
Work verification:
curl -Iv https://localhost.example.com
* Trying 127.0.0.1:443...
* Connected to localhost.example.com (127.0.0.1) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* CAfile: /etc/ssl/certs/ca-certificates.crt
* CApath: none
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-ECDSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: CN=localhost.example.com
* start date: Nov 16 09:18:45 2026 GMT
* expire date: Feb 14 09:18:44 2027 GMT
* subjectAltName: host "localhost.example.com" matched cert's "localhost.example.com"
* issuer: C=US; O=Let's Encrypt; CN=YE2
* SSL certificate verify ok.
> HEAD / HTTP/1.1
> Host: localhost.example.com
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Server: nginx/1.20.2
Server: nginx/1.20.2
< Date: Wed, 16 Nov 2026 11:14:27 GMT
Date: Wed, 16 Nov 2026 11:14:27 GMT
< Content-Type: text/plain; charset=utf-8
Content-Type: text/plain; charset=utf-8
< Content-Length: 11
Content-Length: 11
< Connection: keep-alive
Connection: keep-alive
<
* Connection #0 to host localhost.example.com left intact