acme.sh + Nginx 自动化 SSL 证书与高性能 Web 服务 🚀
🔐 全面指南:自动化 SSL 证书管理 + 高性能 Web 服务器配置
📖 目录导航
🎯 acme.sh 安装配置
🌟 简介
acme.sh 是一个纯 Shell 编写的 ACME 协议客户端,用于自动化 SSL/TLS 证书的申请和续签。它支持多种 DNS 提供商和验证方式,是实现 HTTPS 自动化的理想工具。
📦 安装 acme.sh
1
2
3
4
5
6
7
8
9
10
|
# 方法一:使用 curl 安装(推荐)
curl https://get.acme.sh | sh -s email=meimolihan@live.com
# 方法二:使用 wget 安装
wget -O - https://get.acme.sh | sh -s email=meimolihan@live.com
# 方法三:Git clone 安装
git clone --depth 1 https://github.com/acmesh-official/acme.sh.git
cd acme.sh
./acme.sh --install -m meimolihan@live.com
|
🔧 基本配置
1
2
3
4
5
6
7
8
9
10
11
|
# 重新加载 Shell 配置
source ~/.bashrc
# 创建别名
alias acme.sh=~/.acme.sh/acme.sh
# 查看版本
acme.sh -v
# 设置默认 CA(推荐 Let's Encrypt)
acme.sh --set-default-ca --server letsencrypt
|
🎯 支持的证书颁发机构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# Let's Encrypt(默认)
acme.sh --set-default-ca --server letsencrypt
# Buypass
acme.sh --set-default-ca --server buypass
# ZeroSSL
acme.sh --set-default-ca --server zerossl
# SSL.com
acme.sh --set-default-ca --server ssl.com
# Google PublicCA
acme.sh --set-default-ca --server google
|
🔐 SSL 证书管理
🌐 Cloudflare DNS 验证
1
2
3
4
5
6
7
8
9
|
# 设置 Cloudflare API Token
export CF_Token="1suZKPmVHyHlFJIDv6DsCYS-q_YUeATGZIQo8W8B"
export CF_Zone_ID="382fc112abf99c7994ceaedd4844a243"
# 申请通配符证书
acme.sh --issue --dns dns_cf \
-d "mobufan.eu.org" \
-d "*.mobufan.eu.org" \
--keylength ec-256
|
📁 证书安装
1
2
3
4
5
6
7
8
9
|
# 创建证书目录
sudo mkdir -p /etc/nginx/keyfile
sudo chmod 755 /etc/nginx/keyfile
# 安装证书到 Nginx
acme.sh --install-cert -d mobufan.eu.org \
--key-file /etc/nginx/keyfile/key.pem \
--fullchain-file /etc/nginx/keyfile/cert.pem \
--reloadcmd "systemctl reload nginx"
|
🔄 证书管理命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# 查看所有证书
acme.sh --list
# 查看证书信息
acme.sh --info -d mobufan.eu.org
# 手动续签证书
acme.sh --renew -d mobufan.eu.org --force
# 撤销证书
acme.sh --remove -d mobufan.eu.org
acme.sh --remove -d "*.mobufan.eu.org"
# 开启自动更新
acme.sh --upgrade --auto-upgrade
|
⏰ 自动续签配置
1
2
3
4
5
|
# 查看自动续签任务
crontab -l
# 添加自动续签(如果不存在)
(crontab -l; echo '10 20 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null') | crontab -
|
🐧 Nginx 安装配置
📦 安装 Nginx
1
2
3
4
5
6
7
8
9
10
|
# Debian/Ubuntu
sudo apt update && sudo apt install -y nginx
# 启动并启用开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
# 验证安装
nginx -v
systemctl status nginx
|
🔧 基本管理命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# 启动 Nginx
sudo systemctl start nginx
# 停止 Nginx
sudo systemctl stop nginx
# 重启 Nginx
sudo systemctl restart nginx
# 重新加载配置
sudo systemctl reload nginx
sudo nginx -s reload
# 检查配置语法
sudo nginx -t
# 查看运行状态
sudo systemctl status nginx
# 启用开机自启
sudo systemctl enable nginx
|
📊 日志管理
1
2
3
4
5
6
7
8
|
# 查看错误日志
sudo tail -f /var/log/nginx/error.log
# 查看访问日志
sudo tail -f /var/log/nginx/access.log
# 使用 journalctl 查看日志
sudo journalctl -u nginx -f
|
🗑️ 卸载 Nginx
1
2
3
4
5
6
|
# 完全卸载 Nginx
sudo apt remove --purge nginx nginx-common -y
sudo apt autoremove -y
# 清理配置文件和日志
sudo rm -rf /etc/nginx /var/log/nginx
|
🔄 反向代理配置
🎯 基本反向代理配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# /etc/nginx/conf.d/xunlei.conf
server {
listen 5553 ssl;
listen [::]:5553 ssl;
server_name xunlei.mobufan.eu.org;
# SSL 配置
ssl_certificate /etc/nginx/keyfile/cert.pem;
ssl_certificate_key /etc/nginx/keyfile/key.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://10.10.10.245:2345;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 启用 WebSocket 支持
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 超时设置
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
}
|
🛡️ 安全增强配置
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 安全头部配置
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
# 限制请求方法
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 405;
}
# 隐藏服务器信息
server_tokens off;
|
📁 配置文件管理
1
2
3
4
5
6
7
8
9
10
11
|
# 备份 Nginx 配置
sudo tar -czvf nginx-backup-$(date +%Y%m%d).tar.gz /etc/nginx/
# 恢复配置
sudo tar -xzvf nginx-backup-20231201.tar.gz -C /
# 检查配置语法
sudo nginx -t -c /etc/nginx/nginx.conf
# 测试特定配置文件
sudo nginx -t -c /etc/nginx/conf.d/xunlei.conf
|
⚡ 性能优化
🚀 Nginx 性能调优
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# /etc/nginx/nginx.conf
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
http {
# 启用 Gzip 压缩
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
# 缓冲区优化
client_body_buffer_size 128k;
client_max_body_size 100m;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
# 超时设置
keepalive_timeout 65;
send_timeout 30;
client_body_timeout 30;
client_header_timeout 30;
# 文件传输优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
}
|
📊 监控和调试
1
2
3
4
5
6
7
8
9
10
11
|
# 查看 Nginx 进程
ps aux | grep nginx
# 查看连接状态
netstat -tuln | grep nginx
# 实时监控连接数
watch -n 1 "netstat -an | grep :443 | wc -l"
# 压力测试
ab -n 1000 -c 100 https://xunlei.mobufan.eu.org:5553/
|
🔧 维护管理
📋 日常维护脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/bin/bash
# nginx-maintenance.sh
LOG_FILE="/var/log/nginx-maintenance.log"
echo "$(date): 开始 Nginx 维护" >> $LOG_FILE
# 检查证书有效期
CERT_EXPIRY=$(openssl x509 -enddate -noout -in /etc/nginx/keyfile/cert.pem | cut -d= -f2)
echo "证书过期时间: $CERT_EXPIRY" >> $LOG_FILE
# 检查 Nginx 配置
if ! nginx -t; then
echo "Nginx 配置检查失败" >> $LOG_FILE
exit 1
fi
# 重载 Nginx
systemctl reload nginx
echo "Nginx 重载完成" >> $LOG_FILE
echo "$(date): 维护完成" >> $LOG_FILE
|
🔄 自动化备份
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/bin/bash
# backup-nginx.sh
BACKUP_DIR="/backup/nginx/$(date +%Y%m%d)"
mkdir -p $BACKUP_DIR
# 备份配置
cp -r /etc/nginx $BACKUP_DIR/
cp -r /var/log/nginx $BACKUP_DIR/logs/
# 备份证书
cp -r /etc/nginx/keyfile $BACKUP_DIR/certs/
# 创建压缩包
tar -czf $BACKUP_DIR/nginx-backup.tar.gz $BACKUP_DIR
echo "备份完成: $BACKUP_DIR/nginx-backup.tar.gz"
|
🎯 计划任务配置
1
2
3
4
5
6
7
8
9
10
11
12
|
# 编辑 crontab
sudo crontab -e
# 添加以下任务:
# 每天凌晨备份
0 2 * * * /usr/local/bin/backup-nginx.sh
# 每周维护
0 3 * * 0 /usr/local/bin/nginx-maintenance.sh
# 监控证书过期
0 8 * * * /usr/local/bin/check-cert-expiry.sh
|
💡 最佳实践
🛡️ 安全建议
1
2
3
4
5
6
7
8
9
10
11
|
# 定期更新软件
sudo apt update && sudo apt upgrade nginx
# 检查文件权限
sudo find /etc/nginx -type f -exec chmod 644 {} \;
sudo find /etc/nginx -type d -exec chmod 755 {} \;
sudo chmod 600 /etc/nginx/keyfile/key.pem
# 防火墙配置
sudo ufw allow 443/tcp comment 'Nginx HTTPS'
sudo ufw allow 80/tcp comment 'Nginx HTTP'
|
📊 监控告警
1
2
3
4
5
6
7
8
|
#!/bin/bash
# check-nginx-status.sh
STATUS=$(systemctl is-active nginx)
if [ "$STATUS" != "active" ]; then
echo "Nginx 服务异常: $STATUS" | mail -s "Nginx 服务告警" admin@example.com
systemctl restart nginx
fi
|
🚨 故障排除
🔍 常见问题解决
1. 证书申请失败
1
2
3
4
5
6
|
# 调试模式运行 acme.sh
acme.sh --issue --dns dns_cf -d mobufan.eu.org --debug
# 检查 DNS 解析
dig mobufan.eu.org
dig TXT _acme-challenge.mobufan.eu.org
|
2. Nginx 启动失败
1
2
3
4
5
6
7
8
9
|
# 检查配置语法
nginx -t
# 查看详细错误信息
nginx -T
# 检查端口占用
sudo lsof -i :80
sudo lsof -i :443
|
3. SSL 证书问题
1
2
3
4
5
|
# 检查证书有效性
openssl x509 -in /etc/nginx/keyfile/cert.pem -text -noout
# 测试 SSL 连接
openssl s_client -connect xunlei.mobufan.eu.org:5553 -servername xunlei.mobufan.eu.org
|
4. 性能问题
1
2
3
4
5
6
7
8
|
# 查看 Nginx 工作进程
ps aux | grep nginx
# 监控连接状态
ss -tuln | grep nginx
# 检查系统资源
top -p $(pgrep -d',' nginx)
|
📝 诊断工具
1
2
3
4
5
6
7
8
9
|
# 安装诊断工具
sudo apt install -y net-tools lsof dnsutils
# 网络诊断
ping mobufan.eu.org
traceroute mobufan.eu.org
# SSL 测试
curl -vI https://mobufan.eu.org
|
🔧 紧急恢复
1
2
3
4
5
6
7
8
9
|
# 恢复备份配置
sudo cp -f /backup/nginx/nginx.conf /etc/nginx/
sudo cp -f /backup/nginx/keyfile/* /etc/nginx/keyfile/
# 重启服务
sudo systemctl restart nginx
# 强制续签证书
acme.sh --renew -d mobufan.eu.org --force
|
📋 日志分析
1
2
3
4
5
6
7
8
|
# 实时监控错误日志
tail -f /var/log/nginx/error.log
# 分析访问日志
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10
# 查找错误请求
grep -E '50[0-9]|40[0-9]' /var/log/nginx/access.log | head -20
|
📚 文档和资源
🎯 提示: 建议在生产环境部署前充分测试所有配置。定期检查日志和监控状态,确保服务稳定运行。
🚀 扩展功能:
- 🔄 负载均衡配置
- 🌐 CDN 集成
- 📊 访问日志分析
- 🛡️ WAF 防火墙
- 📱 移动端优化
📞 故障支持:
1
2
3
4
|
# 紧急恢复命令
sudo systemctl restart nginx
sudo acme.sh --renew -d mobufan.eu.org --force
sudo cp -f /backup/nginx/keyfile/* /etc/nginx/keyfile/
|
希望这份完整的 acme.sh + Nginx 指南能帮助您构建安全、高性能的 Web 服务!🎉