Xray 服务端搭建 XTLS + XHTTP 教程
声明:
- 仅供研究和测试,请勿将其用于任何收费或违法活动,后果自负。
- 仅供自用,不一定适合其他人。
特点
- XTLS 和 XHTTP 可直连
- XHTTP 支持过 CDN,并且内层加密,避免中间人 CDN 识别记录
- 均使用 tls,避免使用 REALITY 带来的额外负担,如需要筛选合适域名等
准备工作
- 域名:需要两个域名,一个设置为服务端 IP,另一个开启 CDN 代理到服务端 IP
- 系统:使用 Linux 系系统
步骤
从零开始!从 dd 系统到客户端配置:
配置域名 DNS 记录
以 cloudflare 为例,假设有两个域名:a.example.com 和 a-cdn.example.com
a.example.com:添加记录,指向服务器 IP,代理状态关闭(灰云)
a-cdn.example.com:添加记录,指向服务器 IP,代理状态开启(橙云)
dd 系统
首先,本地电脑生成一个密钥对,供后续使用:
交互式中随意取一个名字即可,比如 xray,然后一路回车即可。
然后获取公钥内容:
随后连接到服务器,运行:
1
| curl -O https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.sh || wget -O ${_##*/} $_
|
准备好公钥和随机生成的密码后,运行:
1
| sudo bash reinstall.sh debian 13 --password 你的密码 --ssh-key "你的公钥内容" --ssh-port 22222
|
进入你的云服务商控制台,放行 22222, 443 端口的入站流量。
编辑本地电脑的 ~/.ssh/config 文件,添加:
1 2 3 4 5
| Host a.example.com HostName a.example.com Port 22222 User root IdentityFile ~/.ssh/xray
|
等待系统安装完成,随后连接到服务器,运行:
注意:后续操作默认使用 root 用户,如果使用其他用户,请自行解决权限问题。
创建假的 sudo 命令(可选)
考虑到全新安装的 Debian 系统可能没有 sudo,部分脚本和指令可能运行失败,虽然不安全,但是图方便可以创建一个假的 sudo 命令:
1 2
| echo -e '#!/bin/bash\nexec "$@"' > /usr/local/bin/sudo chmod +x /usr/local/bin/sudo
|
建设伪装网站
建网站的原因有:
- 申请合法的 TLS 证书
- 提供合理的回落,防止主动探测攻击,提高安全性
- 伪装,使流量使用看上去更合理。
1 2
| mkdir -p /www/webpage bash -c 'echo "<!DOCTYPE html><html><head><title>Welcome to nginx!</title></head><body><h1>Hello World!</h1></body></html>" > /www/webpage/index.html'
|
安装 Nginx
添加 Nginx 官方源,以使用最新特性:
1 2 3 4 5 6
| apt update apt install -y curl gnupg2 ca-certificates lsb-release curl -fsSL https://nginx.org/keys/nginx_signing.key | tee /usr/share/keyrings/nginx-archive-keyring.gpg > /dev/null echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/debian $(lsb_release -cs) nginx" | tee /etc/apt/sources.list.d/nginx.list apt update apt install -y nginx
|
给予 Nginx 访问权限:
1 2 3
| chown -R nginx:nginx /www/webpage mkdir -p /var/log/nginx chown -R nginx:nginx /var/log/nginx
|
首先编写一个最简配置用于 TLS 证书签发:
编辑 Nginx 配置文件 /etc/nginx/nginx.conf,内容如下:
最简 Nginx 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;
events { worker_connections 1024; }
http { server { listen 80; root /www/webpage; index index.html; } }
|
启动 Nginx:
1 2
| systemctl start nginx systemctl enable nginx
|
签发 TLS 证书
安装 acme.sh:
1 2 3 4
| apt install wget -y wget -O - https://get.acme.sh | sh . .bashrc acme.sh --upgrade --auto-upgrade
|
测试证书申请:
1
| acme.sh --issue --server letsencrypt_test -d a.example.com -w /www/webpage --keylength ec-256
|
如果成功,申请正式证书:
1 2 3
| acme.sh --set-default-ca --server letsencrypt acme.sh --issue -d a.example.com -w /www/webpage --keylength ec-256 --force acme.sh --issue -d a-cdn.example.com -w /www/webpage --keylength ec-256 --force
|
安装证书:
1 2 3
| mkdir -p /etc/tlscerts acme.sh --install-cert -d a.example.com --ecc --key-file /etc/tlscerts/a.key --fullchain-file /etc/tlscerts/a.crt acme.sh --install-cert -d a-cdn.example.com --ecc --key-file /etc/tlscerts/a-cdn.key --fullchain-file /etc/tlscerts/a-cdn.crt
|
添加脚本续期后自动安装证书:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| tee /etc/tlscerts/renew.sh > /dev/null << 'EOF'
/root/.acme.sh/acme.sh --install-cert -d a.example.com --ecc --key-file /etc/tlscerts/a.key --fullchain-file /etc/tlscerts/a.crt /root/.acme.sh/acme.sh --install-cert -d a-cdn.example.com --ecc --key-file /etc/tlscerts/a-cdn.key --fullchain-file /etc/tlscerts/a-cdn.crt echo "Certificates Renewed"
chmod +r /etc/tlscerts/*.key chmod +r /etc/tlscerts/*.crt echo "Read Permission Granted for Key"
EOF chmod +x /etc/tlscerts/renew.sh /etc/tlscerts/renew.sh
|
定期执行脚本:
1
| (crontab -l 2>/dev/null; echo "0 0 * * * /etc/tlscerts/renew.sh") | crontab -
|
安装 Xray
下载运行安装脚本:
1 2
| curl -O https://raw.githubusercontent.com/team-cloudchaser/tempest/main/install/xray.sh || wget -O ${_##*/} $_ bash xray.sh
|
给予 Xray 访问权限:
1 2
| mkdir -p /var/log/xray chown -R xray:xray /var/log/xray
|
设置证书文件夹可读权限
1 2 3 4 5 6
| groupadd tlsread usermod -aG tlsread nginx usermod -aG tlsread xray chown -R root:tlsread /etc/tlscerts chmod -R 750 /etc/tlscerts find /etc/tlscerts -type f -exec chmod 640 {} \;
|
编辑配置
可使用 xray 生成 uuid 和 vlessenc:
vlessenc 示例输出:
1 2 3
| Authentication: X25519, not Post-Quantum "decryption": "mlkem768x25519plus.native.600s.wLwttFNPpxkWh57WwWsAP9f0sxV5c6Diq4zSgf8IBl8" "encryption": "mlkem768x25519plus.native.0rtt.upH4tIO4bGpSLpNNuIT4COYnc32uMg8rgbF-khFm3GM"
|
编辑 Xray 配置文件 /usr/local/etc/xray/config.json,内容如下:
xray 配置
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| { "log": { "loglevel": "warning", "error": "/var/log/xray/error.log", "access": "/var/log/xray/access.log" }, "inbounds": [ { "port": 443, "protocol": "vless", "settings": { "clients": [ { "id": "048e0bf2-dd56-11e9-aa37-5600024c1d6a", "flow": "xtls-rprx-vision", "email": "[email protected]" } ], "decryption": "none", "fallbacks": [ { "dest": "/dev/shm/uds88.sock", "xver": 1 } ] }, "streamSettings": { "network": "raw", "security": "tls", "tlsSettings": { "rejectUnknownSni": true, "certificates": [ { "ocspStapling": 3600, "certificateFile": "/etc/tlscerts/a.crt", "keyFile": "/etc/tlscerts/a.key" }, { "ocspStapling": 3600, "certificateFile": "/etc/tlscerts/a-cdn.crt", "keyFile": "/etc/tlscerts/a-cdn.key" } ], "minVersion": "1.2", "cipherSuites": "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" } }, "sniffing": { "enabled": false } }, { "listen": "/dev/shm/uds2023.sock,0666", "protocol": "vless", "settings": { "clients": [ { "id": "af7d5cf8-442d-4bb3-8a76-eb367178781d", "email": "[email protected]" } ], "decryption": "mlkem768x25519plus.native.600s.wLwttFNPpxkWh57WwWsAP9f0sxV5c6Diq4zSgf8IBl8" }, "streamSettings": { "network": "xhttp", "xhttpSettings": { "path": "/VLSpdG9k" } }, "sniffing": { "enabled": false } } ], "outbounds": [ { "protocol": "freedom", "settings": {} } ] }
|
编辑 Nginx 配置文件 /etc/nginx/nginx.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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| user nginx; worker_processes auto;
error_log /var/log/nginx/error.log; #错误日志的文件地址
pid /run/nginx.pid;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
#创建自定义变量 $client_ip 实现使用 CDN 后也能获取到客户端真实 IP,其配置如下:(选配) map $http_x_forwarded_for $client_ip { "" $remote_addr; "~*(?P<firstAddr>([0-9a-f]{0,4}:){1,7}[0-9a-f]{1,4}|([0-9]{1,3}\.){3}[0-9]{1,3})$" $firstAddr; }
#启用访问日志,其调整后的配置如下:(选配) log_format main '$client_ip - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main; #访问日志的文件地址。(选配)
sendfile on;
keepalive_timeout 65;
server { listen 80; listen [::]:80; #若无 IPv6,此项可删除。 return 301 https://$host$request_uri; #HTTP 自动跳转 HTTPS,让网站看起来更真实。 }
server { listen 443 quic reuseport; listen [::]:443 quic reuseport; #若无 IPv6,此项也可删除。 listen unix:/dev/shm/uds88.sock proxy_protocol; http2 on; set_real_ip_from unix:; real_ip_header proxy_protocol;
ssl_certificate /etc/tlscerts/a.crt; #修改为自己的证书,绝对路径。 ssl_certificate_key /etc/tlscerts/a.key; #修改为自己的密钥,绝对路径。
ssl_protocols TLSv1.3;
location /VLSpdG9k { #与 VLESS+XHTTP 应用中 path 对应 grpc_pass grpc://unix:/dev/shm/uds2023.sock; #转发给本机 VLESS+XHTTP 监听进程 grpc_set_header Host $host; grpc_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
location / { add_header Alt-Svc 'h3=":443"; ma=86400'; #通告 HTTP/3 server 的可用性 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; #启用 HSTS root /www/webpage; index index.html; } } }
|
注意更改 UUID 和 XHTTP 路径。
启动服务
启动 Xray 然后重启系统:
1 2
| systemctl enable xray reboot
|
客户端配置
这里建议使用 V2RayN 或 V2RayNG 作为客户端。
可直接导入后替换 UUID、地址、路径 等信息。
1 2 3
| vless://[email protected]:443?encryption=none&flow=xtls-rprx-vision&security=tls&sni=a.example.com&fp=chrome&insecure=0&allowInsecure=0&type=tcp&headerType=none#XTLS vless://[email protected]:443?encryption=mlkem768x25519plus.native.0rtt.upH4tIO4bGpSLpNNuIT4COYnc32uMg8rgbF-khFm3GM&security=tls&sni=a.example.com&fp=chrome&insecure=0&allowInsecure=0&type=xhttp&host=a.example.com&path=%2FVLSpdG9k&mode=auto&extra=%7B%22xmux%22%3A%7B%22maxConcurrency%22%3A%2216-32%22%7D%7D#XHTTP vless://[email protected]:443?encryption=mlkem768x25519plus.native.0rtt.upH4tIO4bGpSLpNNuIT4COYnc32uMg8rgbF-khFm3GM&security=tls&sni=a-cdn.example.com&fp=chrome&insecure=0&allowInsecure=0&type=xhttp&host=a-cdn.example.com&path=%2FVLSpdG9k&mode=auto&extra=%7B%22xmux%22%3A%7B%22maxConcurrency%22%3A%2216-32%22%7D%7D#XHTTP-CDN
|
或者使用 V2RayN 或 V2RayNG 手动添加,配置内容如下:
配置截图
参考