部分VPS厂商使用广播IP售卖机器,经常会出现IP使用地与VPS机房地区不同的情况
而这种情况,往往会导致请求CDN域名时返回IP使用地ip,如距离机房过远则延迟极高
我们可以使用SMARTDNS配合EDNS技术来强行解析VPS机房地区的CDN IP地址

教程

示例机器为中国香港VPS,debian12系统,广播IP位置在美国,请求www.google.com延迟约为150ms
我们首先安装smartdns服务

sudo apt update
sudo apt install smartdns

安装成功后,编辑smartdns的配置文件,默认位置为/etc/smartdns/smartdns.conf

nano /etc/smartdns/smartdns.conf

情况1:该文件有默认内容

此时我们只需要在文件的最下方添加以下内容即可:

edns-client-subnet 118.140.0.1/24
server 8.8.8.8 -bootstrap-dns
server-https https://dns.google/dns-query
dualstack-ip-selection yes
speed-check-mode tcp:443

第一行的ip段118.140.0.1/24 为VPS机房所在地的IP段,示例为中国香港ip段
添加完成后按CTRL+O 回车保存 CTRL+X退出

情况2:smartdns.conf文件内容为空

直接输入以下内容:

bind [::]:53
server 8.8.8.8 -bootstrap-dns
server-https https://dns.google/dns-query
edns-client-subnet 118.140.0.1/24
speed-check-mode tcp:443
dualstack-ip-selection yes
cache-size 4096

此时已经配置好了smartdns,我们接下来启用smartdns

systemctl restart smartdns
nano /etc/resolv.conf

将nameserver全部删除后添加并保存:

nameserver 127.0.0.1

接下来再次ping www.google.com应该返回香港ip地址,并且延迟正常。


重启VPS后DNS设置失效问题解决

/etc/resolv.conf 文件会在VPS重启时恢复为默认DNS设置 我们需要对其设置不可变属性

删除原有的系统生成的DNS文件

rm -f /etc/resolv.conf

写入由 SmartDNS 接管的本地配置

echo "nameserver 127.0.0.1" > /etc/resolv.conf

设置不可变属性

chattr +i /etc/resolv.conf

恭喜你已经配置完成啦~
拯救广播IP计划 大成功~~