Xray 部署记录

0. 证书管理

参考:github.com/acmesh-offi...

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

参考:github.com/XTLS/Xray-i...

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 的配置,请确保不会重复编辑。

相关推荐
我爱拉臭臭39 分钟前
趣味编程之分布式系统:负载均衡的“雨露均沾“艺术
运维·负载均衡
极小狐1 小时前
极狐GitLab 议题和史诗创建的速率限制如何设置?
linux·运维·数据库·git·安全·gitlab·极狐gitlab
BASIS-Jack2 小时前
SAP IAS云产品简介
运维
周天天2 小时前
linux 服务器一步部署jar包,并设置开机自启动和使用systemcl进行服务管理
运维·自动化运维
翻滚吧键盘2 小时前
在 Debian 12 中恢复被删除的 smb.conf 配置文件
运维·数据库·debian
BranH2 小时前
Linux系统中的网络管理
linux·运维·服务器
梁萌2 小时前
DevOps-文章目录
运维·devops
极小狐3 小时前
极狐GitLab 账号限制有哪些?
运维·ssh·gitlab·github·极狐gitlab
敲上瘾4 小时前
网络基础(协议,地址,OSI模型、Socket编程......)
linux·运维·服务器·网络·c++·websocket