强曰为道

与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

15 - 故障排查

第十五章:故障排查

15.1 故障排查流程

问题报告
    │
    ▼
① 确认症状
    │  - 错误码?
    │  - 所有用户还是个别?
    │  - 所有站点还是特定站点?
    │
    ▼
② 查看日志
    │  - access.log → 请求/响应状态
    │  - cache.log → Squid 内部错误
    │
    ▼
③ 检查配置
    │  - squid -k parse
    │  - 检查 ACL 顺序
    │
    ▼
④ 检查系统
    │  - 进程状态、资源使用
    │  - 网络连通性、DNS
    │  - 文件描述符、内核参数
    │
    ▼
⑤ 重现与修复
    │  - 使用 curl 复现
    │  - 调整配置
    │  - 验证修复
    │
    ▼
⑥ 记录与预防

15.2 启动问题

15.2.1 Squid 无法启动

# 症状:systemctl start squid 失败
# 检查错误
sudo systemctl status squid
sudo journalctl -u squid -n 50

# 常见原因 1:配置文件语法错误
sudo squid -k parse
# 修复语法错误后重试

# 常见原因 2:端口被占用
ss -tlnp | grep 3128
# 杀掉占用端口的进程或修改 Squid 端口

# 常见原因 3:权限问题
ls -la /etc/squid/squid.conf
ls -la /var/spool/squid/
ls -la /var/log/squid/
# 确保 proxy 用户有读写权限
sudo chown -R proxy:proxy /var/spool/squid/ /var/log/squid/

# 常见原因 4:缓存目录未初始化
sudo squid -z

# 常见原因 5:SSL 证书问题
ls -la /etc/squid/ssl/
# 确保证书文件存在且权限正确

15.2.2 启动后立即退出

# 查看日志
tail -f /var/log/squid/cache.log

# 常见原因:Helper 程序不存在
# 错误信息: "helperOpenServers: /usr/lib/squid/basic_ncsa_auth: No such file"
# 修复:安装对应包或修改 helper 路径

# 常见原因:缓存目录空间不足
df -h /var/spool/squid/
# 清理空间或调整 cache_dir 大小

# 常见原因:文件描述符限制
cat /proc/$(pgrep squid)/limits | grep "Max open files"
# 修改 /etc/security/limits.conf 或 systemd 服务文件

15.3 连接问题

15.3.1 客户端无法连接代理

# 症状:curl: (7) Failed to connect to proxy port 3128

# 1. 检查 Squid 是否运行
sudo systemctl status squid
ps aux | grep squid

# 2. 检查监听端口
ss -tlnp | grep 3128

# 3. 检查防火墙
sudo iptables -L -n | grep 3128
sudo ufw status

# 4. 检查 Squid 绑定地址
# 如果配置了 http_port 192.168.1.1:3128
# 则只能从 192.168.1.1 访问
grep "http_port" /etc/squid/squid.conf

# 5. 测试本地连接
curl -x http://localhost:3128 http://example.com

15.3.2 代理返回 403 Forbidden

# 症状:HTTP/1.1 403 Forbidden

# 1. 查看 access.log 中的拒绝原因
grep "TCP_DENIED" /var/log/squid/access.log | tail -5

# 2. 常见原因:ACL 拒绝
# 检查 http_access 规则顺序
grep -n "http_access" /etc/squid/squid.conf

# 3. 检查客户端 IP 是否在允许的 ACL 中
# 查看客户端 IP
echo "Client IP: $(curl -s http://httpbin.org/ip)"
# 对比 squid.conf 中的 ACL 定义

# 4. 检查 Safe_ports ACL
# 如果目标端口不在 Safe_ports 中,会被拒绝
# 添加缺少的端口
# acl Safe_ports port 80 443 8080 8443 1025-65535

# 5. 测试特定 ACL
squidclient -h localhost mgr:5min | grep "client_http"

15.3.3 代理返回 407 Proxy Authentication Required

# 症状:需要认证但未提供凭据

# 1. 检查认证配置
grep "auth_param" /etc/squid/squid.conf

# 2. 测试认证
curl -x http://localhost:3128 -U user:password http://example.com

# 3. 检查 Helper 是否正常
echo "user password" | /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd
# 应返回 "OK" 或 "ERR"

# 4. 检查密码文件
cat /etc/squid/passwd
# 确保用户名和密码哈希正确

15.3.4 代理返回 502 Bad Gateway

# 症状:上游服务器不可达

# 1. 检查 Squid 到目标的连通性
curl -v http://target-site.com

# 2. 检查 DNS 解析
nslookup target-site.com
dig target-site.com

# 3. 检查 Squid 日志
grep "502\|CONNECT\|ERROR" /var/log/squid/cache.log | tail -10

# 4. 检查 cache_peer 配置
grep "cache_peer" /etc/squid/squid.conf

# 5. 测试到后端的连接
telnet backend-server 80

15.3.5 代理返回 504 Gateway Timeout

# 症状:上游服务器响应超时

# 1. 检查超时配置
grep -E "connect_timeout|read_timeout|request_timeout" /etc/squid/squid.conf

# 2. 增加超时时间
# connect_timeout 60 seconds
# read_timeout 300 seconds

# 3. 检查网络延迟
ping target-site.com
traceroute target-site.com

# 4. 检查后端服务器状态
curl -v --max-time 30 http://backend-server/

15.4 缓存问题

15.4.1 缓存命中率低

# 症状:请求频繁回源

# 1. 查看缓存统计
squidclient -h localhost mgr:info | grep -i "hit\|miss"

# 2. 检查 refresh_pattern 配置
grep "refresh_pattern" /etc/squid/squid.conf

# 3. 检查源站 Cache-Control 头
curl -I http://example.com/ | grep -i "cache-control\|expires\|pragma"

# 4. 常见原因:
#    - refresh_pattern 设置太保守
#    - 源站设置了 no-cache/no-store
#    - 缓存空间不足
#    - 对象太大超过 maximum_object_size

# 5. 检查缓存目录使用率
squidclient -h localhost mgr:storedir

15.4.2 缓存损坏

# 症状:返回过期或错误的内容

# 1. 清除特定 URL 缓存
squidclient -h localhost -m PURGE http://example.com/page.html

# 2. 重建缓存目录
sudo systemctl stop squid
sudo rm -rf /var/spool/squid/*
sudo squid -z
sudo systemctl start squid

# 3. 检查 store.log
tail -100 /var/log/squid/store.log | grep "RELEASE\|SWAPOUT"

15.5 SSL/HTTPS 问题

15.5.1 SSL Bump 证书错误

# 症状:浏览器显示 "不安全" 警告

# 1. 检查 CA 证书
openssl x509 -in /etc/squid/ssl/myCA.pem -noout -dates

# 2. 检查证书数据库
ls -la /var/lib/squid/ssl_db/

# 3. 检查客户端是否信任 CA
# 导入 CA 证书到浏览器或系统信任存储

# 4. 查看 SSL 错误日志
grep -i "ssl\|certificate\|tls" /var/log/squid/cache.log | tail -20

15.5.2 HTTPS 连接失败

# 症状:CONNECT 隧道建立失败

# 1. 检查 CONNECT ACL
grep "CONNECT\|SSL_ports" /etc/squid/squid.conf

# 2. 测试 HTTPS 代理
curl -x http://localhost:3128 https://example.com -v

# 3. 检查 ssl_bump 规则
grep "ssl_bump" /etc/squid/squid.conf

# 4. 检查目标站点证书
openssl s_client -connect example.com:443 -servername example.com

15.6 性能问题

15.6.1 Squid 响应缓慢

# 症状:请求延迟高

# 1. 检查系统资源
top -p $(pgrep -d',' squid)
free -h
iostat -x 1 5

# 2. 检查 Squid 内部状态
squidclient -h localhost mgr:5min | grep "median"

# 3. 检查 DNS 解析延迟
time nslookup example.com

# 4. 检查文件描述符
cat /proc/$(pgrep squid)/limits | grep "Max open files"
ls /proc/$(pgrep squid)/fd | wc -l

# 5. 检查连接数
ss -s
ss -t state established | wc -l

15.6.2 内存不足

# 症状:OOM Killer 杀死 Squid 进程

# 1. 检查内存使用
free -h
squidclient -h localhost mgr:mem

# 2. 减少内存使用
# cache_mem 256 MB  # 减小内存缓存
# maximum_object_size_in_memory 256 KB

# 3. 检查是否有内存泄漏
# 观察进程内存增长
watch -n 5 "ps -p $(pgrep squid) -o pid,rss,vsz,comm"

15.7 调试模式

15.7.1 启用详细日志

# 运行时启用调试
sudo squid -k debug

# 或在配置中设置调试级别
# debug_options ALL,5

# 仅调试特定模块
# debug_options 28,5  # ACL
# debug_options 33,5  # HTTP
# debug_options 61,5  # DNS

# 前台运行调试
sudo systemctl stop squid
sudo squid -N -d 5

15.7.2 特定请求调试

# 使用 curl 模拟请求并查看详细输出
curl -x http://localhost:3128 -v http://example.com 2>&1

# 使用 squidclient 查看请求处理
squidclient -h localhost -p 3128 http://example.com

# 查看请求头
curl -x http://localhost:3128 -I http://example.com

15.8 常见错误代码

错误码含义常见原因解决方案
ERR_ACCESS_DENIED访问被拒绝ACL 不匹配检查 http_access 规则
ERR_CONNECT_FAIL连接失败网络不通/DNS 错误检查网络和 DNS
ERR_DNS_FAILDNS 解析失败DNS 服务器不可用检查 dns_nameservers
ERR_READ_TIMEOUT读取超时源站响应慢增加 read_timeout
ERR_WRITE_ERROR写入错误磁盘满/I/O 错误检查磁盘空间
ERR_INVALID_REQ无效请求客户端请求格式错误检查客户端配置
ERR_NO_RELAY无法转发没有可用的上游检查 cache_peer

15.9 排查命令速查表

# 进程和资源
ps aux | grep squid
top -p $(pgrep -d',' squid)
free -h
df -h

# 网络
ss -tlnp | grep squid
ss -s
ping target-site.com
traceroute target-site.com

# 配置检查
sudo squid -k parse
grep -n "http_access" /etc/squid/squid.conf

# 日志
tail -f /var/log/squid/access.log
tail -f /var/log/squid/cache.log
grep "ERROR\|WARNING" /var/log/squid/cache.log | tail -20

# Squid 状态
squidclient -h localhost mgr:info
squidclient -h localhost mgr:5min
squidclient -h localhost mgr:mem
squidclient -h localhost mgr:active_requests

# 测试连接
curl -x http://localhost:3128 http://example.com
curl -x http://localhost:3128 https://example.com

15.10 本章小结

问题类型排查步骤
启动失败systemctl statussquid -k parse → 检查日志
连接问题检查进程 → 端口 → 防火墙 → ACL
缓存问题查看命中率 → refresh_pattern → 缓存目录
SSL 问题检查证书 → ssl_bump 规则 → 客户端信任
性能问题资源监控 → DNS → 文件描述符 → 内核参数

扩展阅读