Xray 部署记录

0. 证书管理

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

bash 复制代码
apt -y install socat

安装 acme.sh

bash 复制代码
curl https://get.acme.sh | sh -s email=my@example.com

申请一个证书,在域名 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": "vpsadmin@yourdomain.com"
          }
        ],
        "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 的配置,请确保不会重复编辑。

相关推荐
乘云数字DATABUFF4 天前
5分钟部署开源APM Databuff:OpenTelemetry全链路追踪入门实战
运维·后端
荣--6 天前
一键部署不是为了省时间 —— 它是把"买来的 PaaS"变成"自己的平台"的拐点
运维·zabbix·工程化·一键部署·平台化·边界设计
江华森6 天前
动手实战学 Docker — 从零到集群编排完全指南
运维
Avan_菜菜7 天前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https
SelectDB8 天前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
XIAOHEZIcode9 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户03284722207010 天前
如何搭建本地yum源(上)
运维
大树8813 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠13 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质13 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务