设置透明 Squid 代理
本文档介绍了如何设置 Squid 作为透明代理服务器。
假设如下
- 你已经安装了一个运行 Alpine Linux 作为基础的服务器,版本为 Alpine 1.10.6 或更高版本。
- 你的代理服务器将位于 DMZ 区域,与你的客户端所在的网段隔离。其他实现方式请参考此处。
- 为了将来自你的客户端的网络流量透明地重定向到 DMZ 中的代理服务器,你需要配置你的拦截路由器以进行 DNAT 流量。
注意: 如果你正在寻找设置显式 Squid 代理,或者不知道它们之间的区别,请查看此 Wiki 页面
安装 Squid
apk add acf-squid
使用至少以下配置来配置 Squid
注意: 分别将 proxy.example.com 和 mynet acl 替换为你自己期望的主机名和网络子网
## This makes squid transparent in versions before squid 3.1 #http_port 8080 transparent ## For squid 3.1 and later, use this instead http_port 8080 intercept ## Note that you need Squid 3.4 or above to support IPv6 for intercept mode. Requires ip6tables support visible_hostname proxy.example.com cache_mem 8 MB cache_dir aufs /var/cache/squid 900 16 256 # Even though we only use one proxy, this line is recommended # More info: http://www.squid-cache.org/Versions/v2/2.7/cfgman/hierarchy_stoplist.html hierarchy_stoplist cgi-bin ? # Keep 7 days of logs logfile_rotate 7 access_log /var/log/squid/access.log squid cache_store_log none pid_filename /var/run/squid.pid # Web auditors want to see the full uri, even with the query terms strip_query_terms off refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern -i (/cgi-bin/|\?) 0 0% 0 refresh_pattern . 0 20% 4320 coredump_dir /var/cache/squid # # Authentication # # Optional authentication methods (NTLM, etc) can go here # # Access Control Lists (ACL's) # # These settings are recommended by squid acl shoutcast rep_header X-HTTP09-First-Line ^ICY.[0-9] upgrade_http0.9 deny shoutcast acl apache rep_header Server ^Apache broken_vary_encoding allow apache # Standard ACL settings acl QUERY urlpath_regex cgi-bin \? asp aspx jsp acl all src all acl manager proto cache_object acl localhost src 127.0.0.1/32 acl to_localhost dst 127.0.0.0/8 acl SSL_ports port 443 563 8004 9000 acl Safe_ports port 21 70 80 81 210 280 443 563 499 591 777 1024 1022 1025-65535 acl purge method PURGE acl CONNECT method CONNECT # Require authentication #acl userlist proxy_auth REQUIRED acl userlist src 0.0.0.0/0.0.0.0 # Definition of network subnets acl mynet src 192.168.0.0/24 # # Access restrictions # cache deny QUERY # Only allow cachemgr access from localhost http_access allow manager localhost http_access deny manager # Only allow purge requests from localhost http_access allow purge localhost http_access deny purge # Deny requests to unknown ports http_access deny !Safe_ports # Deny CONNECT to other than SSL ports http_access deny CONNECT !SSL_ports # Allow hosts in mynet subnet to access the entire Internet without being # authenticated http_access allow mynet # Denying all access not explicitly allowed http_access deny all http_reply_access allow all icp_access allow all
使用以下命令检查 Squid
squid -k reconfigure
启动 Squid
rc-service squid start
将 Squid 添加到启动序列
rc-update add squid default
记住将端口 8080 添加到允许客户端连接的端口,无论是在你的代理服务器上还是在代理服务器和客户端之间的任何防火墙上。
如果你在分隔代理服务器和客户端的防火墙上运行 Alpine Linux 防火墙,你将需要将来自你的客户端子网端口 80 的所有流量重定向到代理服务器端口 8080,以允许 Web 流量被代理。
如果你正在运行 Shorewall,请将此添加到你的 /etc/shorewall/rules 文件中
注意: 将 loc 和 dmz:172.16.1.2:8080 替换为你的客户端子网区域和代理服务器区域以及在 /etc/shorewall/zones 中定义的 IP。
## This forces all web traffic to be redirected to the proxy on port 8080 DNAT loc dmz:172.16.1.2:8080 tcp 80
并使用以下命令重启 Shorewall
shorewall check
shorewall restart
或者,你可以配置 Squid 监听端口 80。使用这种方法,通常代理服务器配置为“内联”模式,这样由于物理布线,流量必须通过代理服务器(在这种情况下,Squid 通常与路由器在同一物理机上运行,路由器将是客户端的默认网关),或者(如上所述)端口 80 上的流量由路由器或防火墙重定向到代理服务器(但仍然在端口 80 上)。Cisco 路由器上的 WCCP 是以这种方式重定向流量的一种方法。