在 Linux 上构建 Squid 代理服务器

简介(What)

本文介绍如何在 Linux 上安装和配置基础的 Squid 缓存代理服务器,并指导如何让客户端正确连接使用。

为什么使用 Squid(Why)

Squid 是一款功能强大的缓存代理服务器,可以提升网页加载性能,实现 Web 过滤,并加强访问控制。正确配置客户端能确保流量安全且有效地通过代理转发。

如何操作(How)

第一步:安装 Squid

在 RHEL 系发行版(AlmaLinux、Rocky、CentOS)上:

sudo dnf install squid -y

在 Debian 系发行版(Ubuntu)上:

sudo apt update
sudo apt install squid -y

第二步:配置 Squid 代理

编辑配置文件 /etc/squid/squid.conf

sudo nano /etc/squid/squid.conf

最小可运行配置:

http_port 3128

acl localnet src 192.168.0.0/16
http_access allow localnet
http_access deny all

dns_v4_first on

推荐性能优化参数:

cache_mem 64 MB
maximum_object_size_in_memory 512 KB
maximum_object_size 20 MB
access_log /var/log/squid/access.log
cache_log /var/log/squid/cache.log

第三步:启动并启用 Squid 服务

sudo systemctl enable --now squid

第四步:配置防火墙

firewalld:

sudo firewall-cmd --add-port=3128/tcp --permanent
sudo firewall-cmd --reload

UFW:

sudo ufw allow 3128/tcp

第五步:配置客户端设备

Linux(临时 shell 设置):

export http_proxy="http://<squid-ip>:3128"
export https_proxy="http://<squid-ip>:3128"

Linux(系统级持久设置):

编辑 /etc/environment 文件:

http_proxy="http://<squid-ip>:3128"
https_proxy="http://<squid-ip>:3128"

Windows(当前用户,使用 PowerShell):

netsh winhttp set proxy <squid-ip>:3128

重置方法:

netsh winhttp reset proxy

第六步:测试连接

客户端测试:

curl -x http://<squid-ip>:3128 http://example.com

在 Squid 服务器上查看日志:

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

总结(Conclusion)

本指南提供了一个无需 PAC 文件的简单 Squid 代理服务器和客户端配置方式,具备集中控制、日志记录与网络流量管理的灵活性。

标题和URL已复制