Squid代理服务器应用
一、缓存代理概述
- 代理的基本类型
- 传统代理:适用于Internet,需明确指定服务端
- 透明代理:客户机不需要指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将Web访问重定向给代理服务器代理
- 使用代理的好处
- 提高Web访问速度
- 隐藏客户机的真实IP地址
传统代理
资源列表
主机 | 操作系统 | IP | 配置 |
---|---|---|---|
squid | CentOS7 | 192.168.72.131 | 2C4G |
web | CentOS7 | 192.168.72.132 | 2C4G |
systemctl stop firewalld
systemctl disable firewalld
- 关闭内核安全机制
setenforce 0
sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
- 修改主机名
hostnamectl set-hostname squid
hostnamectl set-hostname web
- CentOS7已经停止维护了,这里我用的是华为源
# 阿里
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 网易
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
# 华为
curl -o /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-anon.repo
一、安装 httpd
[root@web ~]# yum -y install httpd
systemctl start httpd
systemctl enable httpd
二、编译安装 Squid
1、安装支持软件
# 上传软件包
[root@squid ~]# ls
anaconda-ks.cfg squid-3.5.23.tar.gz
[root@squid ~]# yum -y install gcc gcc-* perl
2、创建运行用户、组
useradd -M -s /sbin/nologin squid
# 不创建用户的主目录
# 指定用户的登录shell
3、源码编译安装
1、tar解包
tar zxf squid-3.5.23.tar.gz
cd squid-3.5.23
2、配置编译安装
./configure --prefix=/usr/local/squid \
--sysconfdir=/etc --enable-linux-netfilter --enable-async-io=240 \
--enable-default-err-language=Simplify_Chinese --disable-poll \
--enable-epoll --enable-gnuregex
# --prefix=/usr/local/squid:安装目录
# --sysconfdir=/etc:单独将配置文件修改到其他目录
# --enable-linux-netfilter:使用内核过滤
# --enable-async-io=值:异步I/O, 提升存储性能,相当于“--enable-pthreads --enable-storeio= ufs,aufs --with -pthreads --with-aufs-thread=值”
# --enable-default-err-language=Simplify_Chinese:错误信息的显示语言
# --disable-poll 与--enable-epoll:关闭默认使用 poll 模式,开启 epoll 模式提升性能。
# --enable-gnuregex:使用 GNU 正则表达式
make && make install
echo $?
3、优化执行路径
ln -s /usr/local/squid/sbin/* /usr/local/sbin/
ls -l /usr/local/squid/sbin/
三、编辑主配置文件
chown -R squid:squid /usr/local/squid/var/
[root@squid squid-3.5.23]# vi /etc/squid.conf
# 在 59 行 http_port 3128下面添加用户和组
# 最大限制 10
reply_body_max_size 10 MB
cache_effective_user squid
cache_effective_group squid
# 在 57 行 http_access deny all 上面添加
# 允许所有访问
http_access allow all
# 检测语法
[root@squid squid-3.5.23]# squid -k parse
# 初始化缓存目录
[root@squid squid-3.5.23]# squid -z
[root@squid squid-3.5.23]# 2024/08/14 09:37:54 kid1| Set Current Directory to /usr/local/squid/var/cache/squid
2024/08/14 09:37:54 kid1| Creating missing swap directories
2024/08/14 09:37:54 kid1| No cache_dir stores are configured.
# 启动 squid
[root@squid squid-3.5.23]# squid
# 查看端口号
[root@squid squid-3.5.23]# ss -nlpt | grep 3128
LISTEN 0 128 [::]:3128 [::]:* users:(("squid",pid=92208,fd=11))
# 关闭 squid
[root@squid squid-3.5.23]# squid -k kill
1、脚本
[root@squid squid-3.5.23]# vi /etc/init.d/squid
#!/bin/bash
# chkconfig: 2345 90 25
# config: /etc/squid.conf
# pidfile: /usr/local/squid/var/run/squid.pid
# Description: Squid - Internet Object Cache.
PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
start)
netstat -anpt | grep squid &> /dev/null
if [ $? -eq 0 ]
then
echo "squid is running"
else
echo "正在启动 squid..."
$CMD
fi
;;
stop)
$CMD -k kill &> /dev/null
rm -rf $PID &> /dev/null
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -anpt | grep squid
else
echo "Squid is not running."
fi
;;
restart)
$0 stop &> /dev/null
echo "正在关闭 squid..."
$0 start &> /dev/null
echo "正在启动 squid..."
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
*)
echo "用法: $0 {start | stop | restart | reload | check | status}"
;;
esac
# 为脚本添加执行权限
chmod +x /etc/init.d/squid
# 添加服务到系统启动
chkconfig --add /etc/init.d/squid
# 启动 squid
[root@squid squid-3.5.23]# systemctl start squid
[root@squid squid-3.5.23]# ss -nlpt | grep 3128
LISTEN 0 128 [::]:3128 [::]:* users:(("squid",pid=119119,fd=11))
2、测试
# web 节点打开实时日志
[root@web ~]# tail -f /var/log/httpd/access_log
[root@bogon ~]# curl 192.168.72.132
192.168.72.130 - - [14/Aug/2024:10:03:54 +0800] "GET / HTTP/1.1" 403 4897 "-" "curl/7.29.0"
[root@bogon ~]# curl --proxy 192.168.72.131:3128 192.168.72.132
192.168.72.131 - - [14/Aug/2024:10:05:22 +0800] "GET / HTTP/1.1" 403 4897 "-" "curl/7.29.0"
3、客户端配置代理
cat >> /etc/profile << EOF
HTTP_PROXY=http://192.168.72.131:3128
HTTPS_PROXY=http://192.168.72.131:3128
FTP_PROXY=http://192.168.72.131:3128
# 对两个局域网段不使用代理
NO_PROXY=192.168.1.,192.168.4.
export HTTP_PROXY HTTPS_PROXY FTP_PROXY NO_PROXY
EOF
source /etc/profile
透明代理
资源列表
主机 | 操作系统 | IP | 配置 |
---|---|---|---|
squid | CentOS7 | 192.168.72.131 | 2C4G |
web | CentOS7 | 192.168.72.132 | 2C4G |
systemctl stop firewalld
systemctl disable firewalld
- 关闭内核安全机制
setenforce 0
sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
- 修改主机名
hostnamectl set-hostname squid
hostnamectl set-hostname web
- CentOS7已经停止维护了,这里我用的是华为源
# 阿里
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 网易
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
# 华为
curl -o /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-anon.repo
一、安装 httpd
[root@web ~]# yum -y install httpd
systemctl enable httpd --now
1、squid添加一块网卡,设置为仅主机模式
[root@squid ~]# ifconfig ens36
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.126.128 netmask 255.255.255.0 broadcast 192.168.126.255
inet6 fe80::f121:dba5:3cad:653e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c9:ff:bb txqueuelen 1000 (Ethernet)
RX packets 3 bytes 746 (746.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11 bytes 1434 (1.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2、再开一台机器网卡设置成仅主机模式
nmcli con mod ens33 connection.autoconnect yes ipv4.add 192.168.126.131/24 ipv4.gateway 192.168.126.128 ipv4.method man
nmcli con up ens33
二、编译安装 Squid
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p
1、安装支持软件
# 上传软件包
[root@squid ~]# ls
anaconda-ks.cfg squid-3.5.23.tar.gz
[root@squid ~]# yum -y install gcc gcc-* perl
2、源码编译安装
1、tar解包
tar zxf squid-3.5.23.tar.gz
cd squid-3.5.23
2、配置编译安装
./configure --prefix=/usr/local/squid \
--sysconfdir=/etc --enable-linux-netfilter --enable-async-io=240 \
--enable-default-err-language=Simplify_Chinese --disable-poll \
--enable-epoll --enable-gnuregex
# --prefix=/usr/local/squid:安装目录
# --sysconfdir=/etc:单独将配置文件修改到其他目录
# --enable-linux-netfilter:使用内核过滤
# --enable-async-io=值:异步I/O, 提升存储性能,相当于“--enable-pthreads --enable-storeio= ufs,aufs --with -pthreads --with-aufs-thread=值”
# --enable-default-err-language=Simplify_Chinese:错误信息的显示语言
# --disable-poll 与--enable-epoll:关闭默认使用 poll 模式,开启 epoll 模式提升性能。
# --enable-gnuregex:使用 GNU 正则表达式
make && make install
echo $?
3、创建运行用户、组
useradd -M -s /sbin/nologin squid
# 不创建用户的主目录
# 指定用户的登录shell
4、优化执行路径
ln -s /usr/local/squid/sbin/* /usr/local/sbin/
ls -l /usr/local/squid/sbin/
chown -R squid:squid /usr/local/squid/var/
三、编辑主配置文件
[root@squid squid-3.5.23]# vi /etc/squid.conf
# 在 59 行 http_port 3128 处修改
http_port 192.168.126.128:3128 transparent
# 添加用户和组
cache_effective_user squid
cache_effective_group squid
# 在 57 行 http_access deny all 上面添加
# 允许所有访问
http_access allow all
# 检测语法
[root@squid squid-3.5.23]# squid -k parse
# 初始化缓存目录
[root@squid squid-3.5.23]# squid -z
# 启动 squid
[root@squid squid-3.5.23]# squid
# 查看端口号
[root@squid squid-3.5.23]# ss -nlpt | grep 3128
LISTEN 0 128 [::]:3128 [::]:* users:(("squid",pid=92208,fd=11))
四、测试
iptables -t nat -A PREROUTING -i ens36 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128
iptables -t nat -A PREROUTING -i ens36 -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 3128
# web 节点打开实时日志
[root@web ~]# tail -f /var/log/httpd/access_log
[root@bogon ~]# curl 192.168.72.132
192.168.72.130 - - [14/Aug/2024:10:03:54 +0800] "GET / HTTP/1.1" 403 4897 "-" "curl/7.29.0"
拓展代理 tinyproxy
[root@bogon ~]# ls
anaconda-ks.cfg tinyproxy.git.tar.gz
[root@bogon ~]# tar zxf tinyproxy.git.tar.gz
[root@bogon ~]# ls
anaconda-ks.cfg tinyproxy tinyproxy.git.tar.gz
[root@bogon ~]# cd tinyproxy
[root@bogon tinyproxy]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-anon.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1787 100 1787 0 0 4019 0 --:--:-- --:--:-- --:--:-- 4024
[root@bogon tinyproxy]# yum -y install automake gcc
# 执行脚本
./autogen.sh
# 编译安装
./configure
make
make install
# 进入配置文件
[root@bogon tinyproxy]# vi /usr/local/etc/tinyproxy/tinyproxy.conf
# 在 96 行修改日志
LogFile "/var/log/tinyproxy.log"
# 在 200 和 201 行注释 Allow
#Allow 127.0.0.1
#Allow ::1
[root@bogon tinyproxy]# touch /var/log/tinyproxy.log
[root@bogon tinyproxy]# chown nobody:nobody /var/log/tinyproxy.log
[root@bogon tinyproxy]# cd
[root@bogon ~]# tinyproxy
[root@bogon ~]# netstat -nlpt | grep 8888
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 55935/tinyproxy
tcp6 0 0 :::8888 :::* LISTEN 55935/tinyproxy
[root@bogon ~]# tail -f /var/log/tinyproxy.log
git clone https://kkgithub.com/tinyproxy/tinyproxy.git
yum -y install automake gcc
./autogen.sh
./configure
make
make install
# 启动
tinyproxy
# 关闭
pkill tinyproxy
# 配置文件路径
/usr/local/etc/tinyproxy/tinyproxy.conf
# 想允许所有人使用该代理,注释 Allow 选项即可
# 指定LogFile的话,要注意创建文件并且修改文件的归属用户和组