0. 证书管理
bash
apt -y install socat
安装 acme.sh
bash
curl https://get.acme.sh | sh -s [email protected]
申请一个证书,在域名 xxx.example.com 指向服务 80 的前提下,例如配置 nginx 的 default.conf
:
bash
server {
listen 80 default_server;
location / {
return 444;
}
location /.well-known {
alias /etc/nginx/conf.d/.well-known;
}
}
在这个基础上,申请证书,--webroot
指定到 /etc/nginx/conf.d
中
css
acme.sh --issue -d www.mydomain.com --webroot /etc/nginx/conf.d
完成之后,证书生成到 ~/.acme.sh/www.mydomain.com_ecc
中。
之后将证书分发给 xray:
bash
mkdir ~/xray_cert
acme.sh --install-cert -d www.mydomain.com --ecc \
--fullchain-file ~/xray_cert/xray.crt \
--key-file ~/xray_cert/xray.key
然后可以看到 ~xray_cert
中已经有证书:
ruby
root@svry:~/xray_cert# ll
total 20
drwxr-xr-x 2 root root 4096 Apr 19 20:21 ./
drwx------ 10 root root 4096 Apr 19 19:18 ../
-rw-r--r-- 1 root root 4124 Apr 19 20:21 xray.crt
-rw------- 1 root root 227 Apr 19 20:21 xray.key
添加所有用户可读权限:
bash
chmod +r ~/xray_cert/xray.key
然后由于存在 --install-cert
这个步骤并没有加入 crontab,添加一下,新建这个脚本 ~/xray_cert/xray-cert-renew.sh
,记得加 +x 执行权限:
bash
#!/bin/bash
/home/vpsadmin/.acme.sh/acme.sh --install-cert -d a-name.yourdomain.com --ecc --fullchain-file /home/vpsadmin/xray_cert/xray.crt --key-file /home/vpsadmin/xray_cert/xray.key
echo "Xray Certificates Renewed"
chmod +r /home/vpsadmin/xray_cert/xray.key
echo "Read Permission Granted for Private Key"
sudo systemctl restart xray
echo "Xray Restarted"
然后在 crontab -e
中加入:
bash
# 1:00am, 1st day each month, run `xray-cert-renew.sh`
0 1 1 * * bash /home/vpsadmin/xray_cert/xray-cert-renew.sh
1. 在 Linux 远端服务器安装 xray
bash
bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install
生成一个独一无二的 UUID:
bash
xray uuid
2. 配置
参考:xtls.github.io/document/le...
我用的是 Ubuntu。编辑对应机器的 /usr/local/etc/xray/config.json
配置文件:
2.1. 墙外出口:
请参照这个,配合参考文档进行配置:
json
// REFERENCE:
// https://github.com/XTLS/Xray-examples
// https://xtls.github.io/config/
// 常用的 config 文件,不论服务器端还是客户端,都有 5 个部分。外加小小白解读:
// ┌─ 1*log 日志设置 - 日志写什么,写哪里(出错时有据可查)
// ├─ 2_dns DNS-设置 - DNS 怎么查(防 DNS 污染、防偷窥、避免国内外站匹配到国外服务器等)
// ├─ 3_routing 分流设置 - 流量怎么分类处理(是否过滤广告、是否国内外分流)
// ├─ 4_inbounds 入站设置 - 什么流量可以流入 Xray
// └─ 5_outbounds 出站设置 - 流出 Xray 的流量往哪里去
{
// 1_日志设置
"log": {
"loglevel": "warning", // 内容从少到多: "none", "error", "warning", "info", "debug"
"access": "/home/vpsadmin/xray_log/access.log", // 访问记录
"error": "/home/vpsadmin/xray_log/error.log" // 错误记录
},
// 2_DNS 设置
"dns": {
"servers": [
"https+local://1.1.1.1/dns-query", // 首选 1.1.1.1 的 DoH 查询,牺牲速度但可防止 ISP 偷窥
"localhost"
]
},
// 3*分流设置
"routing": {
"domainStrategy": "IPIfNonMatch",
"rules": [
// 3.1 防止服务器本地流转问题:如内网被攻击或滥用、错误的本地回环等
{
"type": "field",
"ip": [
"geoip:private" // 分流条件:geoip 文件内,名为"private"的规则(本地)
],
"outboundTag": "block" // 分流策略:交给出站"block"处理(黑洞屏蔽)
},
{
// 3.2 防止服务器直连国内
"type": "field",
"ip": ["geoip:cn"],
"outboundTag": "block"
},
// 3.3 屏蔽广告
{
"type": "field",
"domain": [
"geosite:category-ads-all" // 分流条件:geosite 文件内,名为"category-ads-all"的规则(各种广告域名)
],
"outboundTag": "block" // 分流策略:交给出站"block"处理(黑洞屏蔽)
}
]
},
// 4*入站设置
// 4.1 这里只写了一个最简单的 vless+xtls 的入站,因为这是 Xray 最强大的模式。如有其他需要,请根据模版自行添加。
"inbounds": [
{
"port": 443,
"protocol": "vless",
"settings": {
"clients": [
{
"id": "", // 填写你的 UUID
"flow": "xtls-rprx-vision",
"level": 0,
"email": "[email protected]"
}
],
"decryption": "none",
"fallbacks": [
{
"dest": 80 // 默认回落到防探测的代理
}
]
},
"streamSettings": {
"network": "tcp",
"security": "tls",
"tlsSettings": {
"alpn": "http/1.1",
"certificates": [
{
"certificateFile": "/home/vpsadmin/xray_cert/xray.crt",
"keyFile": "/home/vpsadmin/xray_cert/xray.key"
}
]
}
}
}
],
// 5*出站设置
"outbounds": [
// 5.1 第一个出站是默认规则,freedom 就是对外直连(vps 已经是外网,所以直连)
{
"tag": "direct",
"protocol": "freedom"
},
// 5.2 屏蔽规则,blackhole 协议就是把流量导入到黑洞里(屏蔽)
{
"tag": "block",
"protocol": "blackhole"
}
]
}
3. 优化
参考文档中关于 BBR 的优化配置过时(buster),实际我用的是 Ubuntu 22.04(jammy),因此采用这个方法:
bash
echo "net.core.default_qdisc=fq" | sudo tee -a /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
注意理解后执行,前面两行会追加两个 sysctl 的配置,请确保不会重复编辑。