Ubuntu 系统搭建 HTTP 代理完整教程(2026 最新版)

前言

本文详细介绍在 Ubuntu 系统上搭建 HTTP 代理服务器的多种方案,重点讲解 Squid 企业级配置,包含访问控制、缓存优化、负载均衡、高可用等高级特性。

一、Squid 企业级完整配置

1.1 完整配置文件示例

# /etc/squid/squid.conf 完整配置

# ==================== 基础配置 ====================
# 监听端口
http_port 3128
http_port 192.168.1.100:3128 transparent

# 主机名
visible_hostname proxy-server

# 管理员邮箱
cache_mgr admin@example.com

# ==================== 网络 ACL ====================
# 定义本地网络
acl localnet src 192.168.1.0/24
acl localnet src 10.0.0.0/8
acl localnet src 172.16.0.0/12

# 定义特殊网络
acl localhost src 127.0.0.1/32
acl trusted_clients src 192.168.1.100
acl trusted_clients src 192.168.1.101

# ==================== 端口 ACL ====================
# 安全端口定义
acl SSL_ports port 443
acl SSL_ports port 563
acl Safe_ports port 80
acl Safe_ports port 21
acl Safe_ports port 443
acl Safe_ports port 563
acl Safe_ports port 70
acl Safe_ports port 210
acl Safe_ports port 1025-65535
acl Safe_ports port 280
acl Safe_ports port 488
acl Safe_ports port 591
acl Safe_ports port 777

# ==================== 方法 ACL ====================
# HTTP 方法控制
acl CONNECT method CONNECT
acl GET method GET
acl POST method POST
acl PUT method PUT
acl DELETE method DELETE

# ==================== 时间 ACL ====================
# 工作时间控制
acl work_hours time M T W T F 9:00-18:00
acl weekend time S A 00:00-23:59

# ==================== 域名 ACL ====================
# 允许访问的域名
acl allowed_domains dstdomain .google.com
acl allowed_domains dstdomain .github.com
acl allowed_domains dstdomain .stackoverflow.com

# 禁止访问的域名
acl blocked_domains dstdomain .facebook.com
acl blocked_domains dstdomain .twitter.com
acl blocked_domains dstdomain .youtube.com

# ==================== URL 正则匹配 ====================
# 禁止访问的 URL 模式
acl blocked_urls url_regex -i "/admin/"
acl blocked_urls url_regex -i "/login.php"
acl blocked_urls url_regex -i "\.exe$"
acl blocked_urls url_regex -i "\.zip$"

# ==================== 内容类型 ====================
# MIME 类型控制
acl binary_files rep_mime_type -i "^application/octet-stream"
acl binary_files rep_mime_type -i "^application/zip"
acl binary_files rep_mime_type -i "^application/x-executable"

# ==================== 访问控制规则 ====================
# 允许本地访问
http_access allow localhost

# 允许受信任客户端
http_access allow trusted_clients

# 拒绝危险方法
http_access deny DELETE
http_access deny PUT

# 只允许工作时间访问(可选)
# http_access deny !work_hours localnet

# 拒绝禁止域名
http_access deny blocked_domains

# 允许允许域名
http_access allow allowed_domains

# 拒绝禁止 URL
http_access deny blocked_urls

# 拒绝二进制文件下载(可选)
# http_access deny binary_files

# 允许本地网络
http_access allow localnet

# 拒绝所有其他
http_access deny all

# ==================== 缓存配置 ====================
# 缓存目录
cache_dir ufs /var/spool/squid 10240 16 256
cache_dir ufs /var/spool/squid2 10240 16 256

# 内存缓存
cache_mem 1024 MB

# 对象大小限制
maximum_object_size 1024 MB
maximum_object_size_in_memory 256 KB

# 缓存替换策略
cache_replace_policy lru

# 缓存有效期
refresh_pattern -i \.(gif|png|jpg|jpeg|ico)$ 1440 90% 10080
refresh_pattern -i \.(css|js|woff|woff2|ttf|eot)$ 1440 90% 10080
refresh_pattern -i \.(html|htm)$ 60 50% 1440
refresh_pattern . 60 50% 10080

# 缓存忽略规则
cache_ignore_headers Set-Cookie Vary
no_cache deny all

# ==================== 带宽管理 ====================
# 延迟池配置
delay_pools 3
delay_class 1 2
delay_class 2 1
delay_class 3 2

# 网络限制
delay_access 1 allow localnet
delay_access 2 allow trusted_clients
delay_access 3 allow all

# 带宽参数(字节/秒)
delay_parameters 1 100000/200000 50000/100000
delay_parameters 2 500000/1000000 250000/500000
delay_parameters 3 50000/100000 25000/50000

# ==================== 日志配置 ====================
# 访问日志格式
logformat squid %ts.%03tu %6tr %>a %Ss/%03>Hs %

1.2 配置说明

配置项 说明 推荐值
http_port 监听端口 3128
cache_dir 缓存目录大小 (MB) 10240
cache_mem 内存缓存 (MB) 1024
max_filedescriptors 最大文件描述符 65536
maximum_object_size 最大缓存对象 (MB) 1024

1.3 高级 ACL 示例

# 基于用户的认证
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic children 10
auth_param basic realm Squid Proxy
auth_param basic credentialsttl 4 hours
acl authenticated proxy_auth REQUIRED

# 基于组的访问控制
acl group_admin proxy_auth admin
acl group_users proxy_auth user1,user2,user3
http_access allow group_admin
http_access allow group_users work_hours

# 基于地理位置(需要外部数据库)
acl china dst /etc/squid/china_ips.txt
http_access allow china

# 基于时间段的带宽控制
delay_class 1 2
delay_access 1 allow localnet
delay_parameters 1 50000/100000 25000/50000

二、Squid 集群与高可用

2.1 多缓存目录配置

# 使用多个磁盘
cache_dir ufs /disk1/squid 10240 16 256
cache_dir ufs /disk2/squid 10240 16 256
cache_dir ufs /disk3/squid 10240 16 256

# 使用 rock 存储类型(支持更大对象)
cache_dir rock /var/spool/squid 10240 16 256

2.2 父子代理架构

# 子代理配置
cache_peer parent-proxy.com parent 3128 0
cache_peer_domain parent-proxy.com !localnet
cache_peer parent-proxy2.com parent 3128 0 backup-only

# 兄弟代理(对等)
cache_peer sibling1.local sibling 3128 3130
cache_peer sibling2.local sibling 3128 3130

# ICP 查询选项
icp_query_timeout 2000
maximum_icp_query_timeout 5000
minimum_icp_query_timeout 100

2.3 负载均衡配置

# 轮询负载均衡
cache_peer proxy1.local parent 3128 0 round-robin
cache_peer proxy2.local parent 3128 0 round-robin
cache_peer proxy3.local parent 3128 0 round-robin

# 基于权重的负载均衡
cache_peer proxy1.local parent 3128 0 weight=5
cache_peer proxy2.local parent 3128 0 weight=3
cache_peer proxy3.local parent 3128 0 weight=2

三、Squid 监控与维护

3.1 实时监控命令

# 查看缓存命中率
squidclient mgr:info

# 查看当前连接
squidclient mgr:current_clients

# 查看 DNS 缓存
squidclient mgr:dns

# 查看内存使用
squidclient mgr:mem

# 查看磁盘使用
squidclient mgr:diskd

# 查看 HTTP 请求统计
squidclient mgr:http_requests

# 查看缓存对象
squidclient mgr:objects

# 重置统计
squidclient -h localhost:3128 mgr:reset

3.2 日志分析脚本

#!/bin/bash
# squid_analyze.sh - Squid 日志分析脚本

LOG_FILE="/var/log/squid/access.log"

echo "=== 访问 Top 10 域名 ==="
awk "{print \$7}" $LOG_FILE | cut -d"/" -f3 | sort | uniq -c | sort -rn | head -10

echo "\n=== 访问 Top 10 IP ==="
awk "{print \$4}" $LOG_FILE | sort | uniq -c | sort -rn | head -10

echo "\n=== 状态码统计 ==="
awk "{print \$6}" $LOG_FILE | cut -d"/" -f1 | sort | uniq -c | sort -rn

echo "\n=== 带宽使用 Top 10 ==="
awk "{print \$10, \$7}" $LOG_FILE | sort -rn | head -10

echo "\n=== 每小时访问量 ==="
awk -F"[\\[\\]:]" "{print \$2}" $LOG_FILE | cut -d" " -f2 | sort | uniq -c

3.3 缓存清理

# 停止 Squid
sudo systemctl stop squid

# 清理缓存
sudo rm -rf /var/spool/squid/*

# 重新初始化
sudo squid -z

# 启动服务
sudo systemctl start squid

# 在线清理(推荐)
squidclient mgr:objects?pattern=*.jpg&method=DELETE

四、Squid 性能调优

4.1 系统级优化

# /etc/security/limits.conf
squid soft nofile 65536
squid hard nofile 65536
squid soft memlock unlimited
squid hard memlock unlimited

# /etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30

# 应用配置
sudo sysctl -p

4.2 Squid 性能参数

# 调整缓存交换目录数量
cache_dir ufs /var/spool/squid 10240 256 256

# 增加内存池
memory_pools on

# 调整 DNS 缓存
dns_cache_size 20000

# 增加并发连接
max_filedescriptors 131072

# 调整缓存替换算法
cache_replace_policy heap GDSF

五、安全加固

5.1 防止代理滥用

# 限制每个 IP 的连接数
acl over_conn_limit maxconn 50
http_access deny over_conn_limit

# 限制请求频率
acl freq_limit max_user_agent 10/10s
http_access deny freq_limit

# 禁止访问内网
acl private dst 10.0.0.0/8
acl private dst 172.16.0.0/12
acl private dst 192.168.0.0/16
http_access deny private

# 禁止访问本地端口
acl local_port dst 127.0.0.1/8:0-65535
http_access deny local_port

5.2 SSL 拦截配置

# 生成 CA 证书
openssl req -new -newkey rsa:2048 -sha256 -days 3650 -nodes -x509 \
  -keyout /etc/squid/ssl/squid-ca.key \
  -out /etc/squid/ssl/squid-ca.pem

# 配置 HTTPS 拦截
https_port 3129 cert=/etc/squid/ssl/squid-ca.pem key=/etc/squid/ssl/squid-ca.key

# SSL 访问控制
acl ssl_ports port 443
acl SSL_check_certificates ssl::server_name .*
sslproxy_check_certificate_cn on

六、故障排查

6.1 常见问题

# 1. Squid 无法启动
sudo squid -k check
sudo squid -k parse
sudo tail -f /var/log/squid/cache.log

# 2. 缓存目录权限问题
sudo chown -R proxy:proxy /var/spool/squid
sudo chmod -R 755 /var/spool/squid

# 3. 端口被占用
sudo netstat -tlnp | grep 3128
sudo lsof -i :3128

# 4. 内存不足
free -h
cat /proc/$(pidof squid)/status | grep Vm

# 5. 连接数过多
squidclient mgr:current_clients | wc -l
netstat -an | grep :3128 | wc -l

6.2 调试模式

# 前台运行调试
sudo squid -N -d 1

# 增加调试级别
sudo squid -d 5

# 查看配置文件解析
sudo squid -k parse -d 1

总结

本文详细介绍了 Squid 企业级配置,包括:

  • 完整的配置文件示例(200+ 行)
  • ACL 访问控制详解
  • 缓存优化与带宽管理
  • 集群与高可用配置
  • 监控与维护脚本
  • 性能调优参数
  • 安全加固措施
  • 故障排查指南

推荐生产环境根据实际需求调整配置参数。

参考链接

发表回复

后才能评论