自建上网配置

作者:

在

第 1 步:安装所需软件nginx

SSH 登录 VPS,执行:

sudo apt update

sudo apt install -y \
    nginx \
    certbot \
    curl \
    ca-certificates \
    openssl

sudo systemctl enable --now nginx
sudo nginx -t

第 2 步:准备证书申请目录

DOMAIN="test.aspu.top"
EMAIL="test@aspu.top"


创建证书验证目录:
sudo mkdir -p /var/www/certbot/.well-known/acme-challenge

先添加仅用于 HTTP 证书验证的 Nginx 配置:

sudo tee "/etc/nginx/conf.d/${DOMAIN}.conf" >/dev/null <<EOF
server {
    listen 80;
    server_name ${DOMAIN};

    location ^~ /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 404;
    }
}
EOF

第 3 步:申请正式 TLS 证书 参考 https://www.aspu.top/archives/266 申请证书

得到 aspu.top_bundle.crt aspu.top.key;

第 4 步:安装 Xray-core

curl -fsSL \
  -o /tmp/install-release.sh \
  https://github.com/XTLS/Xray-install/raw/main/install-release.sh

sudo bash /tmp/install-release.sh install

检查程序是否安装成功:

sudo /usr/local/bin/xray version

输出类似如下
Xray 26.10.21 (Xray, Penetrates Everything.) 1b1647c (go1.27.3 linux/amd64)
A unified platform for anti-censorship.

第 5 步:生成客户端凭据

UUID="$(sudo /usr/local/bin/xray uuid)"
WS_PATH="/$(openssl rand -hex 16)/"

printf '\nDOMAIN=%s\nUUID=%s\nWS_PATH=%s\n' \
  "$DOMAIN" "$UUID" "$WS_PATH"

预期输出类似
DOMAIN=test.aspu.top
UUID=12345678-9033-4fbe-b56e-123456789012
WS_PATH=/1234567890220d61db42ebf704123456/

第 6 步:写入 Xray 服务端配置

sudo tee /usr/local/etc/xray/config.json >/dev/null <<EOF
{
  "log": {
    "loglevel": "warning"
  },
  "inbounds": [
    {
      "listen": "127.0.0.1",
      "port": 10000,
      "protocol": "vless",
      "settings": {
        "clients": [
          {
            "id": "${UUID}",
            "email": "win11-client"
          }
        ],
        "decryption": "none"
      },
      "streamSettings": {
        "network": "ws",
        "security": "none",
        "wsSettings": {
          "path": "${WS_PATH}"
        }
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "freedom",
      "tag": "direct"
    },
    {
      "protocol": "blackhole",
      "tag": "blocked"
    }
  ]
}
EOF
如果要添加多个用户 
sudo nano /usr/local/etc/xray/config.json

"clients": [
  {
    "id": "原来的UUID",
    "email": "user1"
  },
  {
    "id": "新生成的UUID",
    "email": "user2"
  }
]

‘先检查 JSON 配置:

sudo /usr/local/bin/xray run \
  -test \
  -config /usr/local/etc/xray/config.json

确认没有配置错误,再启动 Xray:

sudo systemctl enable --now xray
sudo systemctl restart xray
sudo systemctl status xray --no-pager

第7步: 配置 Nginx:443 转发给 Xray

sudo tee "/etc/nginx/conf.d/${DOMAIN}.conf" >/dev/null <<EOF
server {
    listen 80;
    server_name ${DOMAIN};

    location ^~ /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://\$host\$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name ${DOMAIN};

    ssl_certificate     /etc/nginx/ssl_cert/aspu.top_bundle.crt;
    ssl_certificate_key /etc/nginx/ssl_cert/aspu.top.key;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    location ${WS_PATH} {
        proxy_pass http://127.0.0.1:10000;
        proxy_http_version 1.1;

        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;

        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        proxy_buffering off;
    }

    location / {
        root /var/www/html;
        index index.html;
    }
}
EOF

重启nginx

sudo nginx -t &&sudo systemctl reload nginx

重启xray

sudo systemctl restart xray

检查 VPS 端口

sudo ss -lntp | grep -E ':(80|443|10000)\b'

预期看到:
- Nginx 监听公网地址的 TCP 80、443。
- Xray 只监听 127.0.0.1:10000。

启动防火墙

sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

第8步: windows 客户端 安装 Clash Verge Rev

从官方 GitHub 下载正式发布版:

Clash Verge Rev Releases

https://github.com/Clash-Verge-rev/clash-verge-rev/releases?utm_source=chatgpt.com

注意选择   Clash Verge_2.5.8_x64-setup.exe  这个版本下载

配置文件如下

config.yaml 文件 打开配置目录即可 编辑 config.yaml

# Generated by Clash Verge

mixed-port: 7897
allow-lan: false
mode: rule
log-level: info
ipv6: false
tcp-concurrent: true
unified-delay: true
external-controller: 127.0.0.1:9090
profile:
  store-selected: true
tun:
  enable: true
  stack: mixed
  auto-route: true
  auto-detect-interface: true
  strict-route: true
  dns-hijack:
  - any:53
  - tcp://any:53
  route-exclude-address:
  - 111.111.111.111/32
  - 111.111.111.112/32
dns:
  enable: true
  ipv6: true
  enhanced-mode: fake-ip
  fake-ip-range: 198.18.0.1/16
  fake-ip-range6: fdfe:dcba:9876::1/64
  default-nameserver:
  - 223.5.5.5
  - 119.29.29.29
  proxy-server-nameserver:
  - 223.5.5.5
  - 119.29.29.29
  direct-nameserver:
  - https://doh.pub/dns-query
  - https://dns.alidns.com/dns-query
  direct-nameserver-follow-policy: true
  nameserver:
  - https://dns.google/dns-query#PROXY
  nameserver-policy:
    rule-set:cn_domain:
    - https://doh.pub/dns-query
    - https://dns.alidns.com/dns-query
    rule-set:private_domain:
    - https://doh.pub/dns-query
proxies:
- name: VPS-VLESS-WS-TLS
  type: vless
  server: 111.111.111.111  #your vps ip
  port: 443
  uuid: 12345678-9033-4fbe-b56e-123456789012
  udp: true
  packet-encoding: xudp
  tls: true
  servername: test.aspu.top
  client-fingerprint: chrome
  network: ws
  ws-opts:
    path: /1234567890220d61db42ebf704123456/
    headers:
      Host: test.aspu.top
  ip-version: ipv4
proxy-groups:
- name: PROXY
  type: select
  proxies:
  - VPS-VLESS-WS-TLS
rule-providers:
  private_domain:
    type: http
    behavior: domain
    format: mrs
    interval: 86400
    proxy: PROXY
    url: https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/meta/geo/geosite/private.mrs
  cn_domain:
    type: http
    behavior: domain
    format: mrs
    interval: 86400
    proxy: PROXY
    url: https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/meta/geo/geosite/cn.mrs
  cn_ip:
    type: http
    behavior: ipcidr
    format: mrs
    interval: 86400
    proxy: PROXY
    url: https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/meta/geo/geoip/cn.mrs
rules:
- RULE-SET,private_domain,DIRECT
- IP-CIDR,10.0.0.0/8,DIRECT,no-resolve
- IP-CIDR,172.16.0.0/12,DIRECT,no-resolve
- IP-CIDR,192.168.0.0/16,DIRECT,no-resolve
- IP-CIDR,127.0.0.0/8,DIRECT,no-resolve
- IP-CIDR,169.254.0.0/16,DIRECT,no-resolve
- DOMAIN-SUFFIX,cn,DIRECT
- RULE-SET,cn_domain,DIRECT
- RULE-SET,cn_ip,DIRECT
- GEOIP,CN,DIRECT,no-resolve
- MATCH,PROXY
socks-port: 7898
port: 7899
external-controller-pipe: \\.\pipe\verge-mihomo-sidecar-release-a1595906cba418580c4329db539544e0fd9deda0f5db071e44d8a08721a708e5
secret: set-your-secret
external-controller-cors:
  allow-private-network: true
  allow-origins:
  - tauri://localhost
  - http://tauri.localhost
  - https://yacd.metacubex.one
  - https://metacubex.github.io
  - https://board.zash.run.place
将  
servername: test.aspu.top
Host: test.aspu.top
uuid: 12345678-9033-4fbe-b56e-123456789012
path: /1234567890220d61db42ebf704123456/

改为之前生成的对应的即可

server: 111.111.111.111  #改为你的vps ip

下面这个很重要  就是说以下服务器ip 不经过虚拟网卡直连  不然一些香港服务器可能会走代理 排除后香港服务器直连就连上了
也能避免退出clash时 导致终端ssh连接的服务断开链接    111.111.111.111  改成你需要排除的服务器ip即可
route-exclude-address:
  - 111.111.111.111/32
  - 111.111.111.112/32

下面很重要 如何对指定ip域名进行直连

指定域名直连
rules:
  - DOMAIN-SUFFIX,example.com,DIRECT
  
指定ip  或 IP段 直连

- IP-CIDR,192.0.2.0/24,DIRECT,no-resolve

指定ipv6 直连
- IP-CIDR6,2001:db8::1/128,DIRECT,no-resolve  
  

最后 启动虚拟网卡模式 和系统代理 关闭ipv6

成功

注意 如果连不上 重启一下 sudo systemctl restart xray

最后验证

访问 https://test.aspu.top/1234567890220d61db42ebf704123456/

返回400 错误就OK

测试

curl.exe --max-time 15 -x http://127.0.0.1:7897 https://api.ipify.org

如果代理链路正常,命令会返回经过代理访问互联网时的公网出口 IP,通常应该是 VPS 的出口 IP。

如何知道某个网站走代理 还是直连

在日志页面 输入域名进行过滤 会提示 using DIRECT or Match using PROXY[VPS-VLESS-WS-TLS

注意: 购买VPS 需要选择带三网优化的

中国电信回程CMI/CU,中国联通回程CU / NTT / CMI 均衡,中国移动回程Level3/CU 有优化的线路 不然连接特别卡 网速龟速尤其是晚上不好用.