Squid 的本质:工作在应用层(L7)的 HTTP/HTTPS 代理。客户端应用显式连接 Squid,Squid 根据 HTTP 请求或 HTTPS CONNECT 建立到目标服务的连接,并负责转发、访问控制、日志和缓存。

本文档面向下面这个环境:

1
2
3
4
5
6
7
8
云服务器(有公网 IP)
公网 IP: 1.2.3.4
运行 Squid
代理端口: 3128

本地服务器
需要让 HTTP/HTTPS 出口流量经过云服务器
通过 HTTP_PROXY / HTTPS_PROXY 使用 Squid

目标是:本地服务器访问外部 HTTP/HTTPS 服务时,出口 IP 变成云服务器公网 IP。

适用场景

  • 本地测试环境访问外部 API 时,需要固定为云服务器出口 IP。
  • 只想让某些应用走代理,而不是改系统默认路由。
  • 需要 HTTP/HTTPS 层访问日志、域名控制、代理认证。
  • 需要按应用、按环境变量精细控制代理行为。

不适用场景

  • 访问 SSH、MySQL、Redis 等非 HTTP 协议。
  • 让整个机器所有流量都走云服务器出口。
  • 把本地服务暴露给公网。
  • 组建生产服务器和本地测试环境的私有网络。

这些场景更适合 WireGuard 或 frp。

运行流程

HTTP:

1
2
3
4
5
本地应用
↓ HTTP_PROXY
Squid on 云服务器

目标网站 / 外部 API

HTTPS:

1
2
3
4
5
本地应用
↓ CONNECT api.example.com:443
Squid on 云服务器
↓ TCP 隧道
api.example.com:443

对于普通 HTTPS 代理,Squid 通常只转发加密字节流,不解密具体 HTTP 内容。

安装

云服务器安装 Squid:

1
2
sudo apt update
sudo apt install -y squid

查看服务:

1
sudo systemctl status squid

配置方式一:只允许固定来源 IP

如果本地服务器有固定公网出口 IP,可以只允许该 IP 使用代理。

编辑配置:

1
sudo vim /etc/squid/conf.d/local-test.conf

示例:

1
2
3
4
5
6
7
8
9
10
11
http_port 3128

acl local_test src 本地服务器公网IP/32
acl SSL_ports port 443
acl Safe_ports port 80
acl Safe_ports port 443

http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow local_test
http_access deny all

重启:

1
2
sudo squid -k parse
sudo systemctl restart squid

云服务器安全组开放:

1
TCP 3128

建议只允许本地服务器公网 IP 访问,不要对全网开放。

配置方式二:只允许 VPN 网段使用

如果已经有 WireGuard,更推荐让 Squid 只监听 VPN IP。

假设云服务器 WireGuard IP 是:

1
10.77.0.1

编辑:

1
sudo vim /etc/squid/conf.d/vpn-only.conf

配置:

1
2
3
4
5
6
7
8
9
10
11
http_port 10.77.0.1:3128

acl vpn_clients src 10.77.0.0/24
acl SSL_ports port 443
acl Safe_ports port 80
acl Safe_ports port 443

http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow vpn_clients
http_access deny all

验证配置并重启:

1
2
sudo squid -k parse
sudo systemctl restart squid

这种方式更安全,因为公网不需要开放 3128

本地服务器使用:

1
2
export http_proxy=http://10.77.0.1:3128
export https_proxy=http://10.77.0.1:3128

本地服务器使用代理

临时使用:

1
2
3
4
export http_proxy=http://1.2.3.4:3128
export https_proxy=http://1.2.3.4:3128
export HTTP_PROXY="$http_proxy"
export HTTPS_PROXY="$https_proxy"

如果 Squid 只监听 VPN IP,则使用:

1
2
3
4
export http_proxy=http://10.77.0.1:3128
export https_proxy=http://10.77.0.1:3128
export HTTP_PROXY="$http_proxy"
export HTTPS_PROXY="$https_proxy"

验证出口:

1
2
curl -x "$http_proxy" https://ifconfig.me
curl https://ifconfig.me

如果应用读取环境变量,也可以直接:

1
curl https://ifconfig.me

配置 apt / git / npm

apt 临时走代理:

1
sudo -E apt update

Git 临时走代理:

1
git -c http.proxy="$http_proxy" -c https.proxy="$https_proxy" ls-remote https://github.com/openai/codex.git HEAD

Git 全局代理:

1
2
git config --global http.proxy "$http_proxy"
git config --global https.proxy "$https_proxy"

取消 Git 全局代理:

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

npm 代理:

1
2
npm config set proxy "$http_proxy"
npm config set https-proxy "$https_proxy"

取消 npm 代理:

1
2
npm config delete proxy
npm config delete https-proxy

代理认证

如果必须把 Squid 暴露到公网,建议增加认证。但更推荐通过安全组或 VPN 限制来源。

安装工具:

1
sudo apt install -y apache2-utils

创建账号:

1
sudo htpasswd -c /etc/squid/passwd proxyuser

配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
http_port 3128

auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED

acl SSL_ports port 443
acl Safe_ports port 80
acl Safe_ports port 443

http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow authenticated
http_access deny all

本地使用:

1
2
export http_proxy=http://proxyuser:password@1.2.3.4:3128
export https_proxy=http://proxyuser:password@1.2.3.4:3128

注意:代理密码会出现在 shell history 和环境变量中,生产环境更推荐 VPN 内访问或来源 IP 白名单。

日志和排查

访问日志:

1
sudo tail -f /var/log/squid/access.log

服务日志:

1
journalctl -u squid -f

监听端口:

1
sudo ss -lntp | grep squid

验证配置语法:

1
sudo squid -k parse

和 WireGuard 的区别

Squid 是应用层代理:

1
应用程序 -> HTTP_PROXY -> Squid -> 目标网站

WireGuard 是系统网络层 VPN:

1
应用程序 -> 系统路由 -> wg0 -> 云服务器 NAT -> 目标网站

如果只是希望本地测试环境整体从云服务器出网,WireGuard + NAT 更简单。如果需要 HTTP 代理日志、域名限制、缓存或只让某些应用走代理,Squid 更合适。

安全建议

  • 不要把 Squid 配成开放代理。
  • 公网暴露 3128 时必须限制来源 IP 或启用认证。
  • 更推荐让 Squid 只监听 VPN IP,例如 10.77.0.1:3128
  • 定期查看 access.log,确认没有异常来源。
  • 不要把代理账号密码写入代码仓库。

常见问题

curl 走代理失败

优先检查:

  • 云服务器安全组是否开放 3128
  • Squid 是否监听正确 IP 和端口。
  • http_access 是否允许当前来源 IP。
  • 本地环境变量是否正确。

访问 HTTPS 失败

优先检查:

  • 是否允许 CONNECT443
  • 是否配置了 SSL_ports port 443
  • 目标站点是否被 ACL 拒绝。

出口 IP 没变

说明应用没有走代理。检查:

  • 应用是否读取 HTTP_PROXY / HTTPS_PROXY
  • 是否只设置了小写变量,而应用只读取大写变量。
  • 命令是否显式绕过了代理。

参考