一文搞定 Squid+Clash 部署
环境:阿里云 ECS(Ubuntu)+ Squid + Clash for Windows
仅限开发测试等合法用途,禁止用于访问境外受限网络。
1. 服务器部署 Squid
# 安装
apt update -y && apt install squid apache2-utils -y
# 创建认证账号(-c 仅首次使用)
htpasswd -c /etc/squid/passwd user123 # 密码输 mima123
# 备份原配置
cp /etc/squid/squid.conf /etc/squid/squid.conf.bak
# 写入配置
cat > /etc/squid/squid.conf <<'EOF'
http_port 3128
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic children 5
auth_param basic realm Squid proxy
auth_param basic credentialsttl 2 hours
acl authenticated proxy_auth REQUIRED
http_access allow authenticated
http_access deny all
cache deny all
cache_dir null /tmp
EOF
# 权限 + 启动
chown proxy:proxy /etc/squid/passwd && chmod 640 /etc/squid/passwd
systemctl enable --now squid
systemctl restart squid
# 检查(必须通过)
systemctl status squid # active (running)
ss -tlnp | grep 3128 # *:3128 或 0.0.0.0:3128
squid -k parse # 无报错防火墙 / 安全组:
ufw status若为 active,执行ufw allow 3128/tcp。- 阿里云安全组入方向添加:TCP
3128/3128,授权0.0.0.0/0。
2. Clash 客户端对接
proxies:
- name: my-squid-http
type: http
server: 你的服务器公网IP
port: 3128
username: user123
password: mima123
proxy-groups:
- name: GLOBAL
type: select
proxies:
- my-squid-http
- DIRECT
- REJECTtype必须写http(不是 socks5)。password行下空一行再写proxy-groups:;空格缩进,禁 Tab。
开启:全局模式 → GLOBAL 选中节点 → 打开系统代理(TUN 关闭)。
验证:浏览器访问 http://httpbin.org/ip,返回服务器 IP 即成功。
3. 排错速查
| 现象 | 处理 |
|---|---|
| 测试超时 | 服务 running?端口监听?安全组放行?平台风控? |
| 连接卡 Waiting | 网络拦截:先查安全组,再看平台风控 |
| 返回 407 | 端口通,账号密码错 |
| 本地测端口 | PowerShell:Test-NetConnection IP -Port 3128,True=通 |
4. 阿里云风控(重点)
典型现象:SSH 22 能连,squid 本地监听正常,防火墙关闭,安全组全放行,但所有自定义端口外网 TCP 全超时(换 4444 测也连不上)。
结论:阿里云网关层风控拦截,不是服务器/防火墙/安全组问题。
处理:提交工单,说明「22 端口正常、其余端口外网全超时、本地监听正常,请求核查平台风控」。
临时方案(SSH 隧道):
# Windows PowerShell,保持窗口不关闭
ssh -L 31280:127.0.0.1:3128 root@你的服务器公网IPClash 节点改连 127.0.0.1:31280。
5. 关停 / 重启
# 临时停
systemctl stop squid
# 彻底关停
systemctl stop squid && systemctl disable squid
# 再次开启
systemctl start squid && systemctl enable squid不用时务必关闭服务并删除安全组 3128 规则,防止代理被外部滥用。
连接成功.png