制作公有证书指向本地(localhost)
Testing SSL (LetsEncrypt certificate and loopback domain)
General approach
This sets up a publically-available domain that loops back to localhost IP address 127.0.0.1
. For example, this address could be localhost.kch8.com
if we controlled the kch8.com
domain. This relies on having a public domain name whose DNS records you can control. We can then generate LetsEncrypt certificates for this domain.
Our HTTP server runs on localhost:80
(default HTTP port). This lets us visit http://localhost.kch8.com
in a web browser and see the server running on localhost:80
.
We then run an HTTPS proxy server on kch8:443
(default HTTPS port) that uses the LetsEncrypt certificates we generated for localhost.kch8.com
. Visiting https://localhost.kch8.com
hits the proxy, which returns the correct certificates meaning the browser displays the “Secure” message. The proxy then passes the request through to the HTTP server.
Issues
- it’s very convoluted
- private keys etc need to be distributed to any machine running the server
- must have control of DNS records on domain
- LetsEncrypt certificates expire so the whole process must be repeated to get new ones (steps 5-9 below)
- …?
Steps
Create the loopback A record for
localhost.kch8.com
:In your DNS provider’s control panel:
1 | Type: A |
- After a while, the following command:
1 | $ dig a localhost.kch8.com. |
- Install LetsEncrypt’s certbot so we can generate a valid SSL cert:
1 | brew install certbot |
- Generate a SSL cert for your domain using the DNS challenge type which means you won’t need to have a server running on this domain:
1 | sudo certbot certonly --config-dir . --work-dir . --logs-dir . --manual --preferred-challenges dns |
- Enter the domain name when prompted:
1 | localhost.kch8.com |
Ok with logging? Yes
Add the DNS record and TXT value in your DNS provider’s control panel as requested by certbot. Do not press Enter until it’s been deployed.
There are a few more questions then certbot will have generated files in the directory you ran the command in:
1 | live |
- Use the redbox proxy to point to the generated certificates and start on port 443:
1 | sudo node redbird-proxy.js live/kch8.example.com/ |
- Set the
API_URL
in.env
to:
1 | API_URL=https://locakch8lhost.example.com/api |
- Start HTTP client on port 80 (edit .env to set PORT=80)
1 | sudo npm start |
The HTTPS server should be available without any browser warnings. HTTP version will be available on http://localhost.kch8.com.
制作公有证书指向本地(localhost)
http://www.kch8.top/2024/06/01/publich-access-cert-loopback-domain/