自建DOH详细教程
mosdns 是 Go 编写的超轻量 DNS 服务器(单二进制 ~10MB,内存 <30MB,常驻低),专为自建 DoH/DoT/anti-pollution设计
优势:
原生支持标准 DoH 服务(/dns-query,兼容 GET/POST)。
只转发上游,无 UI、无额外功能,纯轻量。
支持多个上游、fallback 等。
1.下载并安装 mosdns
wget https://github.com/IrineSistiana/mosdns/releases/latest/download/mosdns-linux-amd64.zip
unzip mosdns-linux-amd64.zip
chmod +x mosdns
mv mosdns /usr/local/bin/mosdns
2.创建目录和 config:
mkdir -p /etc/mosdns
cat > /etc/mosdns/config.yaml <<EOF
log:
level: info
plugins:
- tag: upstreams
type: forward
args:
upstreams:
- addr: tls://1.1.1.1
- addr: tls://dns.google
- tag: doh_http
type: http_server
args:
listen: 127.0.0.1:8053
entries:
- path: /dns-query
exec: upstreams # 所有 /dns-query 请求直接执行 upstreams 插件(forward)
idle_timeout: 30
EOF
3.运行并守护
测试运行:
mosdns -c /etc/mosdns/config.yaml
守护进程(用 Supervisor 或 systemd):
cat > /etc/systemd/system/mosdns.service <<EOF
[Unit]
Description=mosdns DoH proxy
After=network.target
[Service]
ExecStart=/usr/local/bin/mosdns -c /etc/mosdns/config.yaml
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now mosdns
apache配置文件添加以下代码进行反代即可绑定域名
<Location /dns-query>
ProxyPass http://127.0.0.1:8053/dns-query
ProxyPassReverse http://127.0.0.1:8053/dns-query
RequestHeader set Upgrade $http_upgrade
RequestHeader set Connection "upgrade"
</Location>