一、前言
Squid 是一个功能强大、稳定可靠的代理服务器软件,本文将详细介绍如何在 Linux 系统上安装配置 Squid,用于爬虫代理。
二、环境准备
2.1 系统要求
-
CentOS 7/8 或 Ubuntu 18.04+
-
至少 1GB 内存
-
公网 IP(云服务器需开放端口)
2.2 安装 Squid
CentOS/RHEL 系统:
# 更新系统 yum update -y # 安装 Squid yum install squid -y # 安装防火墙管理工具 yum install firewalld -yUbuntu/Debian 系统:
bash
# 更新系统 apt update -y # 安装 Squid apt install squid -y
三、基础配置
3.1 配置文件结构
Squid 的主配置文件位于 /etc/squid/squid.conf。我们先备份原始配置:
cp /etc/squid/squid.conf /etc/squid/squid.conf.backup
3.2 最简单的代理配置
cat > /etc/squid/squid.conf << 'EOF' # 监听所有网络接口的 3128 端口 http_port 0.0.0.0:3128 # 设置主机名(避免警告) visible_hostname proxy-server # 允许所有客户端访问(测试用) http_access allow all # 允许 HTTPS CONNECT 请求 acl SSL_ports port 443 acl CONNECT method CONNECT http_access allow CONNECT SSL_ports # 缓存配置(可选) cache_dir ufs /var/spool/squid 100 16 256 cache_mem 128 MB # 日志配置 access_log /var/log/squid/access.log cache_log /var/log/squid/cache.log # 超时设置 forward_timeout 30 seconds connect_timeout 30 seconds read_timeout 30 seconds request_timeout 30 seconds EOF
3.3 初始化缓存目录
# 创建缓存目录 squid -z # 设置权限 chown -R squid:squid /var/spool/squid chown -R squid:squid /var/log/squid
四、高级配置
4.1 IP 访问控制
# 定义允许的网段 acl allowed_network src 192.168.1.0/24 # 允许办公室网络 acl allowed_network src 10.0.0.0/8 # 允许公司内网 acl allowed_network src 172.16.0.0/12 # 允许分公司内网 # 定义单个 IP acl single_ip src 114.212.189.66 # 应用访问控制 http_access allow allowed_network # 允许这些内网段访问 http_access allow single_ip http_access deny all # 拒绝其他所有访问
4.2 用户认证配置
# 安装认证工具 yum install httpd-tools -y # CentOS apt install apache2-utils -y # Ubuntu # 创建密码文件 mkdir -p /etc/squid/passwd htpasswd -c /etc/squid/passwd/htpasswd username1 htpasswd /etc/squid/passwd/htpasswd username2 # 在 squid.conf 中添加认证配置 auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd/htpasswd auth_param basic children 5 auth_param basic realm Squid Proxy Authentication auth_param basic credentialsttl 2 hours # 创建认证 ACL acl auth_users proxy_auth REQUIRED # 允许认证用户访问 http_access allow auth_users
4.3 限制带宽和连接数
# 限制每个 IP 最大连接数 acl limited_ips src 0.0.0.0/0 acl maxconn maxconn 50 http_access deny limited_ips maxconn # 限制带宽(单位 KB/s) delay_pools 1 delay_class 1 1 delay_parameters 1 500000/500000 delay_access 1 allow all
4.4 优化性能配置
# 增加 DNS 缓存 dns_nameservers 8.8.8.8 114.114.114.114 dns_v4_first on positive_dns_ttl 6 hours negative_dns_ttl 1 minute # 内存缓存设置 cache_mem 256 MB maximum_object_size_in_memory 512 KB # 磁盘缓存设置 cache_dir ufs /var/spool/squid 2048 16 256 max_open_disk_fds 0 # 超时优化 read_timeout 15 minutes client_lifetime 1 day
五、启动和管理服务
5.1 服务管理
# 检查配置文件语法 squid -k parse # 启动服务 systemctl start squid # 设置开机自启 systemctl enable squid # 重启服务 systemctl restart squid # 查看状态 systemctl status squid # 重新加载配置(不中断服务) squid -k reconfigure
5.2 防火墙配置
# CentOS 7/8 (firewalld) firewall-cmd --zone=public --add-port=3128/tcp --permanent firewall-cmd --reload # Ubuntu (iptables) iptables -A INPUT -p tcp --dport 3128 -j ACCEPT iptables-save > /etc/iptables/rules.v4
5.3 云服务器安全组配置
如果是云服务器,还需在控制台配置安全组:
-
协议:TCP
-
端口:3128
-
来源:0.0.0.0/0(或指定 IP)
-
优先级:100
六、日志管理
6.1 实时监控
# 查看访问日志 tail -f /var/log/squid/access.log # 查看缓存日志 tail -f /var/log/squid/cache.log # 统计访问量 cat /var/log/squid/access.log | wc -l
6.2 日志轮转
# 手动轮转日志 squid -k rotate # 配置自动轮转(/etc/logrotate.d/squid) cat > /etc/logrotate.d/squid << 'EOF' /var/log/squid/*.log { daily rotate 7 compress missingok notifempty sharedscripts postrotate /sbin/squid -k rotate endscript } EOF
七、 curl 命令行测试使用
# HTTP 请求 curl -x http://211.149.137.70:3128 http://httpbin.org/ip # HTTPS 请求 curl -x http://211.149.137.70:3128 https://httpbin.org/ip # 带认证 curl -x http://username:password@211.149.137.70:3128 https://httpbin.org/ip # 自定义 User-Agent curl -x http://211.149.137.70:3128 \ -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" \ https://httpbin.org/ip
八、常见问题排查
8.1 连接超时
# 检查服务状态 systemctl status squid # 检查端口监听 netstat -tlnp | grep 3128 # 应该看到 0.0.0.0:3128,而不是 :::3128
8.2 性能优化
# 查看缓存命中率 squidclient mgr:info # 增加 worker 进程数(多核 CPU) workers 4
九、安全建议
-
生产环境务必配置认证,避免被滥用
-
限制访问 IP,仅允许可信来源
-
开启访问日志,便于审计
-
定期更新 Squid,修复安全漏洞
-
设置合理的带宽限制,防止耗尽资源
十、总结
本文详细介绍了在 Linux 系统上安装配置 Squid 代理服务器的完整过程。Squid 作为成熟的代理软件,配置灵活、性能稳定。
关键步骤回顾:
-
安装 Squid
-
配置监听地址和访问控制
-
开放防火墙端口
-
启动服务并验证
注:本文章仅用于技术学习使用。