温馨提示×

centos如何限速域名

小樊
42
2026-10-14 13:56:14
栏目: 云计算

在 CentOS 上“限速某个域名”通常不是直接由系统内核完成的,因为 iptables/nftables 基于 IP 和端口,不解析域名。常见做法是:

先解析域名 → 得到 IP → 对 IP 限速

下面按常见场景给你几种方案。


一、使用 tc + iptables(推荐,纯 CentOS 原生)

思路

  1. 解析域名得到 IP
  2. 用 iptables 打标记
  3. 用 tc 对标记流量限速

示例:限制访问 example.com(假设解析为 93.184.216.34)

1. 解析域名

dig +short example.com

2. 给该 IP 打标记

iptables -t mangle -A OUTPUT -d 93.184.216.34 -j MARK --set-mark 10

3. 使用 tc 限速(例如限速 1Mbps)

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 + tc(CentOS 7/8)

如果你用 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 配置同上。


三、用 Squid 代理限速(HTTP/HTTPS 域名级)

这是唯一“真正按域名限速”的方案

适用

  • 内网代理
  • 明确域名(如 youtube.com、*.example.com)

步骤

  1. 安装 Squid
yum install squid -y
  1. 配置限速
acl limit_domain dstdomain .example.com
delay_pools 1
delay_class 1 1
delay_parameters 1 125000/125000
delay_access 1 allow limit_domain
  1. 启动
systemctl start squid

✅ 可按域名
❌ 必须走代理


四、用 nftables + tc(CentOS 8+)

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

如果你能说明:

  • CentOS 版本
  • 是 入站还是出站
  • 是否走代理 / 容器 / 网关

我可以直接给你一套完整可执行配置。

0 踩