自建 Trojan 节点中 SSL 证书路径配置详解

大家好,我是 33blog 的博主。今天想和大家分享我在自建 Trojan 节点时,关于 SSL 证书路径配置的一些经验和踩坑记录。SSL 证书配置是 Trojan 服务搭建中最容易出错的一环,很多新手都会在这里卡住。通过这篇文章,我将带你一步步理清证书路径配置的要点。
为什么 SSL 证书对 Trojan 如此重要
Trojan 协议的核心就是通过 TLS 加密来伪装流量,让防火墙难以识别。如果没有正确配置 SSL 证书,Trojan 服务就无法正常启动,客户端也无法连接。我在第一次搭建时就因为证书路径写错,折腾了大半天才找到问题所在。
获取 SSL 证书的两种方式
在配置路径之前,你需要先获得 SSL 证书。我推荐两种方式:
1. 使用 Let’s Encrypt 免费证书
这是最常用的方式,通过 Certbot 工具自动获取和续期:
# 安装 Certbot
sudo apt update
sudo apt install certbot
# 获取证书(将 yourdomain.com 替换为你的域名)
sudo certbot certonly --standalone -d yourdomain.com
2. 自签名证书(仅测试使用)
如果你只是想在本地测试,可以生成自签名证书:
# 生成私钥和证书
openssl req -newkey rsa:2048 -nodes -keyout server.key -x509 -days 365 -out server.crt
Trojan 配置文件中的证书路径设置
这是最关键的部分。Trojan 的配置文件(通常是 config.json)中需要正确指定证书和密钥的路径。以下是我的配置示例:
{
"run_type": "server",
"local_addr": "0.0.0.0",
"local_port": 443,
"remote_addr": "127.0.0.1",
"remote_port": 80,
"password": ["your_password"],
"ssl": {
"cert": "/etc/letsencrypt/live/yourdomain.com/fullchain.pem",
"key": "/etc/letsencrypt/live/yourdomain.com/privkey.pem",
"key_password": "",
"cipher": "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:...",
"prefer_server_cipher": true,
"alpn": ["http/1.1"],
"reuse_session": true,
"session_ticket": false,
"session_timeout": 600,
"plain_http_response": ""
}
}
常见路径问题及解决方法
在实际操作中,我遇到过几个典型的路径问题:
1. 权限问题
Let’s Encrypt 证书默认只有 root 用户可读,但 Trojan 可能以其他用户运行:
# 将证书目录权限设置为可读
sudo chmod 755 /etc/letsencrypt/live/
sudo chmod 755 /etc/letsencrypt/archive/
2. 路径不存在
确保你写的路径确实存在,可以通过以下命令检查:
# 检查证书文件是否存在
ls -la /etc/letsencrypt/live/yourdomain.com/
3. 软链接问题
Let’s Encrypt 使用软链接,某些情况下需要确保软链接正确:
# 检查软链接
ls -la /etc/letsencrypt/live/yourdomain.com/
证书自动续期配置
Let’s Encrypt 证书只有 90 天有效期,设置自动续期很重要:
# 测试续期
sudo certbot renew --dry-run
# 设置定时任务
sudo crontab -e
# 添加以下行(每天检查续期)
0 12 * * * /usr/bin/certbot renew --quiet
验证配置是否正确
配置完成后,我习惯用以下命令验证:
# 检查 Trojan 配置语法
trojan -t /path/to/your/config.json
# 重启 Trojan 服务
sudo systemctl restart trojan
# 查看服务状态
sudo systemctl status trojan
如果服务正常运行,恭喜你!SSL 证书路径配置已经成功。如果遇到问题,记得查看日志文件:
sudo journalctl -u trojan -f
总结
SSL 证书路径配置看似简单,但细节决定成败。通过这篇文章,我希望你能避免我当初踩过的坑。记住:路径要绝对路径、注意文件权限、定期检查证书有效期。如果你在配置过程中遇到其他问题,欢迎在评论区留言讨论!


证书路径写错真的会卡半天,深有体会!
Let’s Encrypt自动续期太重要了,不然90天就断了 😅