Squid代理服务器搭建及入门使用

如何搭建Squid代理服务器?

Squid代理服务器搭建及入门使用
Photo by John Cardamone / Unsplash

1、Squid简介

Squid是一款开源代理服务器和Web缓存加速器,主要用于缓存和加速网络请求。形象地说,它是网络信息的中转站,是个人网络和Internet服务上之间的中间代理机构。

Squid官网
GitHub

2、Squid安装

2.1 基础环境

操作系统:Ubuntu 22.04.3 LTS

2.2 安装

使用apt进行安装

sudo apt update && apt install squid -y

安装完成后查看状态

sudo systemctl status squid

2.3 配置

默认配置文件位于/etc/squid/squid.conf,修改配置文件前,最好将原文件备份

sudo cp /etc/squid/squid.conf /etc/squid.squid.conf.bak

使用文本编辑器打开squid.conf文件

sudo vim /etc/squid/squid.conf

2.3.1 配置监听端口

默认情况下,Squid监听3128端口,建议修改默认端口,默认端口容易被扫描到

http_port 3218

2.3.2 身份验证

Squid支持多种身份验证方式,常用的是Basic认证(基于用户名密码)。可以使用ncsa_auth进行Basic认证。

1.安装Apache工具(包含htpasswd

sudo apt install apache2-utils -y

2.创建用户密码文件

htpasswd -c /etc/squid/passwords hello

要求输入两次密码,成功设置后文件/etc/squid/passwords包含了hello及其密码。

修改文件权限,确保文件可读

sudo chmod o+r /etc/squid/passwords

3.修改配置文件/etc/squid/squid.conf,末尾添加以下内容:

acl allcomputers src 0.0.0.0/0.0.0.0
auth_param basic program /usr/lib/squid3/basic_ncsa_auth /etc/squid3/passwords
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated allcomputers

找到http_access deny all并注释:

# http_access deny all

2.3.3 禁止本机访问

当启用身份验证,在本机安装转发工具时,通过tcp转发请求到Squid中,可以忽略验证。注释以下配置,可以实现tcp方式连接需身份验证。

http_access allow localhost

2.3.4 防止泄露版本信息

httpd_suppress_version_string on

2.3.5 验证代理是否可用

Squid访问日志位于/var/log/squid/access.log

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

找一台可以访问到代理服务器的linux机器,配置代理信息

export http_proxy="http://用户名:密码@代理服务器IP:端口号"
curl -s http://www.baidu.com

如果配置正确,会显示html代码,在access.log中记录访问记录。

2.4 配置高匿代理

/etc/squid/squid.conf中加上

forwarded_for delete
via off
follow_x_forwarded_for deny all
request_header_access From deny all
request_header_access Server deny all
request_header_access WWW-Authenticate deny all
request_header_access Link deny all
request_header_access Cache-Control deny all

至此,已经完成了Squid的基本搭建及使用。