Deploy certificates
After a certificate is deployed in the Certificate Center (green dot), you still need to select it once where it's used.
| Where it's used | How to use it | After an auto certificate renews |
|---|---|---|
| CDN domains | Select in the console | Updated automatically |
| Bound bucket domains | Select in the console | Updated automatically |
| Your own servers, other platforms | Fetch through the API | Fetched on a schedule by your script |
In Associated projects (关联项目) in the list, you can see which CDN domains and bucket domains use a certificate.
Use for a CDN domain
- Go to CDN › Static acceleration management (静态加速管理) (open) and click Manage (管理) on the domain.
- Switch to HTTPS configuration (HTTPS 配置) and click Edit (编辑) on the HTTPS configuration card.
- Select On (开启), choose the certificate in the Certificate (证书) drop-down, and click Save (保存).
- Wait for the status to go from Deploying (部署中) back to Running (运行中), which usually takes 30 seconds.
If the certificate doesn't cover the domain or has expired, saving fails and the reason is shown. For the other options, see HTTPS settings.
Use for a bound bucket domain
Creating new bucket domains is paused. Connect new domains to CDN. Bucket domains that were bound earlier can still have a certificate set:
- Go to Object storage › Bucket list › a specific bucket › Website settings (对象存储 › 桶列表 › 具体的桶 › 网站设置) and click Manage domain (管理域名) under Bound domains (绑定域名).
- On the HTTPS configuration card, click Edit, select On, and choose the certificate in the Certificate drop-down.
- Click Save.
Notes:
- Once on, HTTP/2, OCSP, and TLS 1.0 to 1.3 are enabled by default, with no separate switches here
- Confirm yourself that the certificate covers this domain
- After you replace your own certificate, select it again here and save once
- A certificate that is deploying or in an abnormal state can't be selected. You see "该证书正在部署中,请稍后" (this certificate is being deployed, try again later) or "证书状态异常" (abnormal certificate status)
Use for your own servers
Fetch the certificate and private key through the Certificate API, write them to the server, and reload the service. With a scheduled job, after an auto certificate renews, the certificate on your server is updated automatically too.
Prepare an API token
Create one in Console › API Token. See API tokens. A token can read the private keys of all certificates in your account, so keep it only on the server and restrict the file permissions.
Save the script
You need curl, jq, and openssl installed. Change the certificate name (copy it from the certificate list), the save path, and the reload command.
#!/usr/bin/env bash
set -euo pipefail
umask 077
CERT_NAME="my-cert" # The certificate name in the Certificate Center
TOKEN="$(cat /etc/bitiful/api-token)" # Readable only by root, permissions 600
DIR="/etc/nginx/certs/cdn.example.com"
mkdir -p "$DIR"
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
# URL-encode the certificate name if it has special characters
NAME="$(jq -rn --arg n "$CERT_NAME" '$n|@uri')"
# Pass the request header through stdin so the token doesn't appear in the process list
printf 'Authorization: %s\n' "$TOKEN" |
curl -fsS --retry 3 --max-time 30 -H @- \
"https://api.bitiful.com/cert/data/${NAME}/content" -o "$TMP/cert.json"
jq -er .crt "$TMP/cert.json" > "$TMP/fullchain.pem"
jq -er .key "$TMP/cert.json" > "$TMP/private.key"
# Don't replace if the certificate doesn't match the private key, or has expired
openssl x509 -in "$TMP/fullchain.pem" -noout -checkend 0 > /dev/null
[ "$(openssl x509 -in "$TMP/fullchain.pem" -noout -pubkey)" = \
"$(openssl pkey -in "$TMP/private.key" -pubout)" ]
# Exit if nothing changed
if cmp -s "$TMP/fullchain.pem" "$DIR/fullchain.pem" && cmp -s "$TMP/private.key" "$DIR/private.key"; then
exit 0
fi
install -m 644 "$TMP/fullchain.pem" "$DIR/fullchain.pem"
install -m 600 "$TMP/private.key" "$DIR/private.key"
nginx -t && systemctl reload nginxVerify
echo | openssl s_client -connect cdn.example.com:443 -servername cdn.example.com 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates -ext subjectAltNameCheck that the validity period is the new certificate's, and that subjectAltName includes the domain being accessed.
FAQ
Can one certificate be used for multiple domains?
Yes. As long as the certificate covers those domains, you can use it on multiple CDN domains, bucket domains, and servers.
After an auto certificate renewed, the CDN domain still shows the old validity period
The new certificate is pushed automatically, and it takes a little time. Use the command above to confirm the certificate actually in use. If the CDN domain uses a different certificate, the renewal doesn't affect it.
The API returns 403
A 403 is returned when the certificate name doesn't exist, the certificate is still being requested, or the request failed. Confirm the name and status in the list.