Squid Web Cache Wiki

Squid Web Cache 文档

🔗 在 Linux 路由器上策略路由 Web 流量

🔗 目录

本示例概述了如何配置 Linux 路由器以将流量(在本例中为 Web 流量)策略路由到 Squid 代理。

🔗 用法

各种网络使用嵌入式 Linux 设备(如 OpenWRT)作为网关,并希望实现透明缓存或代理。

Linux 中没有明显的策略路由 - 您使用 iptables 标记感兴趣的流量,使用 iproute2 ip 规则选择备用路由表,并在备用路由表中使用默认路由来策略路由到发行版。

请注意,这只会将数据包发送到代理;您必须在代理本身上配置拦截才能将流量重定向到 Squid TCP 端口!

🔗 iptables 设置

🔗 当 Squid 在客户端内部时

# IPv4 address of proxy
PROXYIP4= 192.168.0.10

# IPv6 address of proxy
PROXYIP6= fe80:dead:beef::10

# interface facing clients
CLIENTIFACE= eth0

# arbitrary mark used to route packets by the firewall. May be anything from 1 to 64.
FWMARK= 2


# permit Squid box out to the Internet
iptables -t mangle -A PREROUTING -p tcp --dport 80 -s $PROXYIP4 -j ACCEPT
ip6tables -t mangle -A PREROUTING -p tcp --dport 80 -s $PROXYIP6 -j ACCEPT

# mark everything else on port 80 to be routed to the Squid box
iptables -t mangle -A PREROUTING -i $CLIENTIFACE -p tcp --dport 80 -j MARK --set-mark $FWMARK
iptables -t mangle -A PREROUTING -m mark --mark $FWMARK -j ACCEPT
ip6tables -t mangle -A PREROUTING -i $CLIENTIFACE -p tcp --dport 80 -j MARK --set-mark $FWMARK
ip6tables -t mangle -A PREROUTING -m mark --mark $FWMARK -j ACCEPT

# NP: Ensure that traffic from inside the network is allowed to loop back inside again.
iptables -t filter -A FORWARD -i $CLIENTIFACE -o $CLIENTIFACE -p tcp --dport 80 -j ACCEPT
ip6tables -t filter -A FORWARD -i $CLIENTIFACE -o $CLIENTIFACE -p tcp --dport 80 -j ACCEPT

🔗 当 Squid 在路由器和互联网之间的 DMZ 中时

注意:仅当 Squid 服务器不是路由器的正常网关时,才需要此特殊配置。如果您将 Squid 服务器设置为默认网关,并将所有流量通过它从路由器导出,则这些规则不是必需的。但是,Squid 和内核将争夺 CPU 周期来处理它们各自的流量部分,这会使两者都有些减速。

# interface facing clients
CLIENTIFACE= eth0

# arbitrary mark used to route packets by the firewall. May be anything from 1 to 64.
FWMARK= 2


# mark everything on port 80 to be routed to the Squid box
iptables -t mangle -A PREROUTING -i $CLIENTIFACE -p tcp --dport 80 -j MARK --set-mark $FWMARK
iptables -t mangle -A PREROUTING -m mark --mark $FWMARK -j ACCEPT
ip6tables -t mangle -A PREROUTING -i $CLIENTIFACE -p tcp --dport 80 -j MARK --set-mark $FWMARK
ip6tables -t mangle -A PREROUTING -m mark --mark $FWMARK -j ACCEPT

🔗 路由设置

需要以 root 身份运行。

cat /etc/iproute2/rt_tables

选择一个尚未存在的数字。我们在此演示中选择 201。您需要选择自己的。

为我们拦截的代理流量创建一个路由表

echo "201   proxy" >> /etc/iproute2/rt_tables

配置由该表处理的流量(由 iptables 在前面标记为 2 的内容),并为其创建一个默认路由到位于 $PROXYIP 的 Squid 服务器。

ip rule add fwmark 2 table proxy
ip route add default via $PROXYIP table proxy

🔗 Squid 配置

Squid 是一个独立的服务器,对吧?有关配置它的详细信息,请参阅 ConfigExamples/Intercept 的 **捕获到 Squid** 部分。


⚠️ Disclaimer: Any example presented here is provided "as-is" with no support
or guarantee of suitability. If you have any further questions about
these examples please email the squid-users mailing list.

类别: ConfigExample

导航:站点搜索站点页面分类🔼 向上