使用 acme.sh 设置 SSL 证书
本指南提供了使用 acme.sh 脚本设置 SSL 证书的分步说明,其中 Let’s Encrypt 为默认证书颁发机构 (CA)。它包括用于安装、配置和颁发根域和通配符域证书的命令。
步骤概述
- 安装 acme.sh:下载并安装
acme.sh脚本。提供用于接收通知的电子邮件地址。
bash
curl https://get.acme.sh | sh -s email=d342jxc@gmail.com- 设置默认 CA:配置
acme.sh使用 Let’s Encrypt 作为默认 CA。
bash
acme.sh --set-default-ca --server letsencrypt- 别名配置:为
acme.sh添加别名,以简化命令使用并重新加载shell配置。
bash
echo 'alias acme.sh=~/.acme.sh/acme.sh' >> ~/.bashrc
source ~/.bashrc- Cloudflare API 凭证:添加 Cloudflare API 凭证(
CF_Key和CF_Email)以启用基于 DNS 的域验证。
bash
export CF_Key="xxx"
export CF_Email="xxx@gmail.com"- 颁发证书:使用
--issue命令通过 Cloudflare DNS 验证为根域和通配符子域生成 SSL 证书。
bash
acme.sh --issue --dns dns_cf -d example.com -d *.example.com- 安装证书:将生成的证书安装到指定路径,并为Nginx Web服务器配置重新加载命令。
bash
acme.sh --install-cert -d 'example.com' \
--key-file /etc/nginx/ssl/example.com/example.com.key \
--fullchain-file /etc/nginx/ssl/example.com/example.com.crt \
--reloadcmd "service nginx force-reload"bash
acme.sh --install-cert -d '*.example.com' \
--key-file /etc/nginx/ssl/example.com/example.com.key \
--fullchain-file /etc/nginx/ssl/example.com/example.com.crt \
--reloadcmd "service nginx force-reload"注意事项
- 将
xxx和xxx@gmail.com替换为您的实际 Cloudflare API 密钥和电子邮件地址。 - 将
example.com替换为您的实际域名。 - 确保密钥和证书文件的指定路径与您的 Nginx 配置相匹配。
--reloadcmd选项可确保在证书续订后自动重新加载 Nginx。
此设置可确保根域和所有子域的安全 HTTPS 连接。
反向代理
nginx
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Range $http_range;
proxy_set_header If-Range $http_if_range;
proxy_redirect off;
proxy_pass https://bangumi.tv;
# the max size of file to upload
client_max_body_size 20000m;
}
location ^~ /xui {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Range $http_range;
proxy_set_header If-Range $http_if_range;
proxy_redirect off;
proxy_pass http://127.0.0.1:1000;
# the max size of file to upload
client_max_body_size 20000m;
}
location ^~ /alist {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Range $http_range;
proxy_set_header If-Range $http_if_range;
proxy_redirect off;
proxy_pass http://127.0.0.1:5244;
# the max size of file to upload
client_max_body_size 20000m;
}


