测试环境:Ubuntu 20.04

前置条件:拥有一个域名、一台服务器、把域名解析到服务器IP

安装V2Ray

官方文档:https://guide.v2fly.org/prep/install.html

sudo apt update
sudo apt install curl
curl -O https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh
sudo bash install-release.sh
v2ray -version
# V2Ray 4.39.2 (V2Fly, a community-driven edition of V2Ray.) Custom (go1.16.4 linux/amd64)
# A unified platform for anti-censorship.

# 返回以上内容表示安装成功

安装Caddy v2

Caddy服务器是一个开源的,使用 Golang 编写,支持 HTTP/2 的 Web 服务端。它使用Golang 标准库提供HTTP 功能。 Caddy 一个显著的特性是默认启用HTTPS。它是第一个无需额外配置即可提供HTTPS 特性的Web 服务器。

官方文档:https://caddyserver.com/docs/install#debian-ubuntu-raspbian

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo apt-key add -
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
caddy version
# v2.4.1 h1:kAJ0JB5Xk5gPdTH/27S5cyoMGqD5lBAe9yZ8zTjVJa0=

# 返回以上内容表示安装成功

配置V2Ray服务端

官方文档:https://guide.v2fly.org/advanced/h2_tls_web.html#web-%E6%9C%8D%E5%8A%A1%E5%99%A8

编辑/usr/local/etc/v2ray/config.json文件,写入如下内容

UUID可在这里生成:https://www.uuidgenerator.net/version1

{
  "inbounds": [
    {
      "port": 44302,
      "listen":"127.0.0.1",
      "protocol": "vmess",
      "settings": {
        "clients": [
          {
            "id": "你的UUID",
            "alterId": 0
          }
        ]
      },
      "streamSettings": {
        "network": "ws",
        "wsSettings": {
        "path": "/ray"
        }
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "freedom",
      "settings": {}
    }
  ]
}
service v2ray restart
# 启动v2ray

service v2ray status
# 查看状态

systemctl enable v2ray
# 允许开机自启

配置Caddy v2

创建Caddyfilev2ray文件

vim /etc/caddy/Caddyfilev2ray

写入以下内容

/usr/share/caddy是你的网站源文件目录

example.com改成你的域名

example.com {
    root * /usr/share/caddy
    file_server
    log {
        output file /etc/caddy/caddy.log
    }
    tls {
        protocols tls1.2 tls1.3
        ciphers TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
        curves x25519
    }
    @v2ray_websocket {
        path /ray
        header Connection *Upgrade*
        header Upgrade websocket
    }
    reverse_proxy @v2ray_websocket localhost:44302
}
caddy stop
caddy start --config /etc/caddy/Caddyfilev2ray --adapter caddyfile
# 启动caddy

设置Caddy开机自启

编辑/lib/systemd/system/rc-local.service,在文件尾部添加以下内容

[Install]
WantedBy=multi-user.target

创建/etc/rc.local文件并输入以下内容

#!/bin/sh -e
# 
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# caddy v2ray start
/usr/bin/caddy start --config /etc/caddy/Caddyfilev2ray --adapter caddyfile
/usr/bin/caddy reload --config /etc/caddy/Caddyfilev2ray

exit 0 # 如果文件末尾不存在exit 0,请添加此行
sudo chmod +x /etc/rc.local
# 添加执行权限

systemctl enable rc-local.service
# 启用rc-local服务

V2Ray客户端配置

{
  "inbounds": [
    {
      "port": 1080,
      "listen": "0.0.0.0",
      "protocol": "socks",
      "sniffing": {
        "enabled": true,
        "destOverride": [
          "http",
          "tls"
        ]
      },
      "settings": {
        "auth": "noauth",
        "udp": false
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "vmess",
      "settings": {
        "vnext": [
          {
            "address": "你的域名",
            "port": 443,
            "users": [
              {
                "id": "你的UUID",
                "alterId": 64
              }
            ]
          }
        ]
      },
      "streamSettings": {
        "network": "ws",
        "security": "tls",
        "wsSettings": {
          "path": "/ray"
        }
      }
    }
  ]
}

路由规则

路由规则:https://github.com/Loyalsoldier/v2ray-rules-dat

cd /usr/local/share/v2ray/
wget https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geoip.dat
wget https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geosite.dat
...