v2ray + CDN + Nginx 拯救被墙IP

由于GFW的原因,IP被封,所以只能另辟蹊径:

  • v2ray是一个集成了各种科学上网协议的软件,包括socks(目前接触到的是本机到本机)、http、SS(目前接触到的是本机到远程)、vmess等,传输载体可以是tcp、mkcp、websocket等
  • 使用websocket:因为cdn可以转发ws流量
  • 使用https(tls)伪装网站,请求流量数据
  • 使用cdn转发流量,同时作为连接vps的跳板,使vps复活和隐藏
  • 使用nginx代理v2ray

准备工作

  • 一台vps (IP被墙,IP假设为:99.99.99.99)
  • 域名www.xyz.com
  • 免费cdn:cloudfare
  • 相关知识:nginx配置,ssl配置,域名配置等

安装nginx

添加yum源:

1
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安装:

1
yum install nginx

相关服务命令:

1
2
3
4
5
6
7
8
9
10
11
设置开机启动
systemctl enable nginx

启动服务
systemctl start nginx

重启服务
systemctl restart nginx

重载服务
systemctl reload nginx

编辑配置文件:
相关配置文件位置/etc/nginx/下,替换server_name为

1
xyz.com www.xyz.com;

然后启动nginx服务。

申请证书

申请免费https证书(Let’s Encrypt),先在域名注册商那里(GoDaddy)修改dns,添加两个a记录,解析到vps的ip 99.99.99.99,修改的原因是要向let‘s encrypt要发起挑战,证明你拥有这个域名。
下载certbot:

1
2
git clone https://github.com/certbot/certbot
cd certbot

生成免费证书:

1
2
3
./certbot-auto certonly --webroot --agree-tos -v -t --email 邮箱地址 -w 网站根目录 -d 网站域名
例如:
./certbot-auto certonly --webroot --agree-tos -v -t --email liwen@163.com -w /usr/share/nginx/html -d www.xyz.com

注:生成过程中会自动生成 /网站根目录/.well0known/acme-challenge,然后脚本会挑战这个路径,所以需要保证这个地址能被访问,才能正常生成免费的证书。
为了保证能访问,需要开启nginx,同时,需要关闭dns(箭头不穿过小云朵)。

生成的证书地址:

1
2
/etc/letsencrypt/live/www.xyz.com/fullchain.pem
......等等

配置nginx证书:

1
2
3
4
5
listen 443

ssl on;
ssl_certificate /etc/letsencrypt/live/网站域名/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/网站域名/privkey.pem;

cdn设置

设置完证书后,就可以设置cdn

注册cloudfare账号,选择一个免费站点

更换godaddy处的nameserver为cloudfare的nameserver,把解析工作转移给cloudfare

等待cloudfare网站变为active状态

安装v2ray

vps安装配置v2ray

安装v2ray:

1
2
3
# 官方一键安装脚本
bash <(curl -l -s https://install.direct/go.sh)
安装好后,会有v2ray运行的端口,和一个client的uuid port:26075 uuid:563a2749-ccfe-4754-959d-b8343faafeac (记住上述信息,并使用实际的更换)

编辑配置文件:

1
vi /etc/v2ray/config.json

在inbound的最后(settings之后)追加如下配置,记住path路径:

1
2
3
4
5
6
7
8
"listen": "0.0.0.0",

"streamsettings": {
"network": "ws",
"wssettings": {
"path": "/gotowork"
}
}

重启v2ray:

1
systemctl restart v2ray

继续设置nginx:
继续设置nginx,反向代理到v2ray

编辑nginx配置文件

1
2
3
4
5
6
7
8
9
# 添加类似如下设置
location /gotowork { # 路径为上面的路径
proxy_redirect off;
proxy_pass http://127.0.0.1:26075; # 端口要变成v2ray运行的端口
proxy_http_version 1.1;
proxy_set_header upgrade $http_upgrade;
proxy_set_header connection "upgrade";
proxy_set_header host $http_host;
}

重启nginx:

1
systemctl restart nginx

客户端配置

macos客户端选择v2rayx:
https://github.com/cenmrev/v2rayx
最终配置如下:

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{
"routing": {
"name": "all_to_main",
"domainStrategy": "AsIs",
"rules": [
{
"type": "field",
"outboundTag": "cdnv2ray",
"port": "0-65535"
}
]
},
"inbounds": [
{
"listen": "127.0.0.1",
"protocol": "socks",
"settings": {
"ip": "127.0.0.1",
"auth": "noauth",
"udp": true
},
"tag": "socksinbound",
"port": 1081
},
{
"listen": "127.0.0.1",
"protocol": "http",
"settings": {
"timeout": 0
},
"tag": "httpinbound",
"port": 8001
}
],
"dns": {
"servers": [
"8.8.8.8",
"8.8.4.4"
]
},
"log": {
"error": "/var/folders/95/q73tyvts04s_g3pddf07tfjw0000gn/T/cenmrev.v2rayx.log/error.log",
"loglevel": "warning",
"access": "/var/folders/95/q73tyvts04s_g3pddf07tfjw0000gn/T/cenmrev.v2rayx.log/access.log"
},
"outbounds": [
{
"sendThrough": "0.0.0.0",
"mux": {
"enabled": false,
"concurrency": 8
},
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": "www.xyz.com", # 注意
"users": [
{
"id": "e4sdf75b-349b-43c6-hj98-9228f5fdfdsf3", # 注意
"alterId": 64,
"security": "auto",
"level": 1
}
],
"port": 443
}
]
},
"tag": "cdnv2ray",
"streamSettings": {
"wsSettings": {
"path": "/gotowork", # 注意
"headers": {}
},
"quicSettings": {
"key": "",
"security": "none",
"header": {
"type": "none"
}
},
"tlsSettings": {
"allowInsecure": false,
"alpn": [
"http/1.1"
],
"serverName": "www.xyz.com", # 注意
"allowInsecureCiphers": false
},
"httpSettings": {
"path": ""
},
"kcpSettings": {
"header": {
"type": "none"
},
"mtu": 1350,
"congestion": false,
"tti": 20,
"uplinkCapacity": 5,
"writeBufferSize": 1,
"readBufferSize": 1,
"downlinkCapacity": 20
},
"tcpSettings": {
"header": {
"type": "none"
}
},
"security": "tls", # 注意
"network": "ws" # 注意
}
}
]
}

相关链接

https://blog.sprov.xyz/2019/03/11/cdn-v2ray-safe-proxy/#i-9
https://www.dazhuanlan.com/2019/10/04/5d962810eb6c8/