在 CentOS 上“限速某个域名”通常不是直接由系统内核完成的,因为 iptables/nftables 基于 IP 和端口,不解析域名。常见做法是:
先解析域名 → 得到 IP → 对 IP 限速
下面按常见场景给你几种方案。
iptables 打标记tc 对标记流量限速dig +short example.com
iptables -t mangle -A OUTPUT -d 93.184.216.34 -j MARK --set-mark 10
tc qdisc add dev eth0 root handle 1: htb
tc class add dev eth0 parent 1: classid 1:10 htb rate 1mbit ceil 1mbit
tc filter add dev eth0 protocol ip parent 1:0 prio 1 handle 10 fw flowid 1:10
✅ 优点:稳定、原生
❌ 缺点:域名 IP 变化需手动更新
如果你用 firewalld:
firewall-cmd --permanent --direct --add-rule ipv4 mangle OUTPUT 0 -d 93.184.216.34 -j MARK --set-mark 10
firewall-cmd --reload
然后 tc 配置同上。
这是唯一“真正按域名限速”的方案
yum install squid -y
acl limit_domain dstdomain .example.com
delay_pools 1
delay_class 1 1
delay_parameters 1 125000/125000
delay_access 1 allow limit_domain
systemctl start squid
✅ 可按域名
❌ 必须走代理
nft add table inet limit
nft add chain inet limit output { type route hook output priority 0; }
nft add rule inet limit output ip daddr 93.184.216.34 meta mark set 10
再配合 tc。
for ip in $(dig +short example.com); do
iptables -t mangle -A OUTPUT -d $ip -j MARK --set-mark 10
done
可配合 cron 定时更新。
| 需求 | 推荐方案 |
|---|---|
| 服务器出口限速 | tc + iptables |
| 内网用户访问域名 | Squid |
| 简单稳定 | IP + tc |
| 现代系统 | nftables |
如果你能说明:
我可以直接给你一套完整可执行配置。