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 https://get.acme.sh | sh -s email=meimolihan@live.com
wget -O - https://get.acme.sh | sh -s email=meimolihan@live.com
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
| source ~/.bashrc
alias acme.sh=~/.acme.sh/acme.sh
acme.sh -v
acme.sh --set-default-ca --server letsencrypt
|
🎯 支持的证书颁发机构
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| acme.sh --set-default-ca --server letsencrypt
acme.sh --set-default-ca --server buypass
acme.sh --set-default-ca --server zerossl
acme.sh --set-default-ca --server ssl.com
acme.sh --set-default-ca --server google
|
🔐 SSL 证书管理
🌐 Cloudflare DNS 验证
1 2 3 4 5 6 7 8 9
| 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
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
| 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
| sudo systemctl start nginx
sudo systemctl stop 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
sudo journalctl -u nginx -f
|
🗑️ 卸载 Nginx
1 2 3 4 5 6
| 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
| server { listen 5553 ssl; listen [::]:5553 ssl; server_name xunlei.mobufan.eu.org; 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; 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
| 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
| events { worker_connections 1024; multi_accept on; use epoll; }
http { 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
| 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
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
if ! nginx -t; then echo "Nginx 配置检查失败" >> $LOG_FILE exit 1 fi
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_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
| 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
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 --issue --dns dns_cf -d mobufan.eu.org --debug
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
openssl s_client -connect xunlei.mobufan.eu.org:5553 -servername xunlei.mobufan.eu.org
|
4. 性能问题
1 2 3 4 5 6 7 8
| 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
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 服务!🎉
acme.sh + Nginx 自动化 SSL 证书与高性能 Web 服务 🚀