squid正向代理安装部署

squid简介

Squid是一个高性能的代理缓存服务器,Squid支持FTP、gopher、HTTPS和HTTP协议,和一般的代理缓存软件不同,Squid用一个单独的、非模块化的、I/O驱动的进程来处理所有的客户端请求

web代理的工作机制

缓存网页对象,减少重复请求

在这里插入图片描述
代理的基本类型

  • 传统代理:适用于Internet,需明确指定服务器
  • 透明代理:客户机不需要指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将web访问重定向给代理服务器处理

使用代理的好处

  • 提高web访问速度
  • 影藏客户机的真实IP地址

搭建传统代理服务器

1、环境部署
(1)环境拓扑
在这里插入图片描述
(2)IP地址规划

主机名 IP地址
squid代理 192.168.7.128
Web 192.168.7.129
客户机 192.168.7.155

2、squid安装
(1)安装环境包

[root@squid ~]# yum install gcc gcc-c++ -y

(2)编译安装squid安装包

[root@squid squid]# tar zxvf squid-3.4.6.tar.gz -C /opt
[root@squid squid]# cd /opt/squid-3.4.6/
[root@squid squid-3.4.6]# ./configure \
--prefix=/usr/local/squid					//指定安装路径
--sysconfdir=/etc							//指定配置文件目录
--enable-arp-acl							//启用acl访问控制列表功能
--enable-linux-netfilter					//内核过滤
--enable-linux-tproxy						//支持透明模式
--enable-async-io=100						//io优化
--enable-err-language="Simplify_Chinese"	//设定支持语言
--enable-underscore							//支持下划线
--enable-poll								//开启poll功能
--enable-gnuregex							//支持正则表达式

[root@squid squid-3.4.6]# make && make install

(3)创建软链接

[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/

(4)创建suid用户,更改/usr/local/squid/var/的属主、属组

[root@squid ~]# useradd -M -s /sbin/nologin squid
[root@squid ~]# chown -R squid.squid /usr/local/squid/var/

(5)更改配置文件

[root@squid ~]# vim /etc/squid.conf
http_access allow all
#http_access deny all
#添加指定用户
cache_effective_user squid		
#添加指定组
cache_effective_group squid		
#容灾备份目录(此项不需修改)
coredump_dir /usr/local/squid/var/cache/squid	

(6)检查配置文件语法

[root@squid ~]# squid -k parse

(7)初始化缓存目录

[root@squid ~]# squid -z

(8)启动服务

[root@squid ~]# squid

(9)创建squid管理脚本

[root@squid ~]# cd /etc/init.d/
[root@squid init.d]# vim squid
#!/bin/bash
#chkconfig: 2345 90 25
PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"

case "$1" in
   start)
     netstat -natp | 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 -natp | grep squid
        else
          echo "squid is not running"
        fi
     ;;
   restart)
     $0 stop &> /dev/null
     echo "正在关闭 squid..."
     $0 start &> /dev/null
     echo "正在启动 squid..."
     ;;
   check)
     $CMD -K parse
     ;;
   *)
     echo "用法: $0{start|stop|status|reload|check|resatrt}"
esac

[root@squid init.d]# chmod +x squid 
[root@squid init.d]# chkconfig --add squid
[root@squid init.d]# chkconfig --level 35 squid on

3、配置传统代理服务器
(1)更改squid配置文件

[root@squid ~]# vim /etc/squid.conf
#插入以下内容
#指定缓存功能所使用的的内存空间大小
cache_mem 64 MB			
#允许用户下载的最大文件大小
reply_body_max_size 10 MB	
#允许保存到缓存空间的最大文件大小
maximum_object_size 4096 KB	

#重启服务
[root@squid ~]# service squid restart

(2)设置squid防火墙规则

[root@squid ~]# iptables -F
[root@squid ~]# iptables -t nat -F
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

4、web服务器配置

  • 安装Apache服务器
[root@web ~]# yum install httpd -y
[root@web ~]# systemctl start httpd

5、客户端测试
(1)更改浏览器的代理服务器
在这里插入图片描述
(2)使用浏览器访问web网页
在这里插入图片描述
(3)在web服务器上打开日志文件,查看访问网页的IP地址

[root@web ~]# tail -f /var/log/httpd/access_log 
192.168.7.128 - - [18/Mar/2020:11:14:33 +0800] "GET / HTTP/1.1" 200 26 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
192.168.7.128 - - [18/Mar/2020:11:14:33 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://192.168.7.129/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"

透明模式代理配置

1、IP地址规划,在squid服务器上配置双网卡,内置网卡ens33,外置网卡ens36

主机名 IP地址
squid代理 ens33:192.168.7.128,ens36:192.168.10.1
Web 192.168.7.129
客户机 192.168.10.10

2、在squid上添加路由转发功能

[root@squid ~]# vim /etc/sysctl.conf
#在最后一行添加
net.ipv4.ip_forward=1

[root@squid ~]# sysctl -p
net.ipv4.ip_forward=1

3、更改squid主配置文件,设置防火墙规则

[root@squid ~]# vim /etc/squid.conf
http_port 192.168.10.1:3128 transparent

#重启服务
[root@squid ~]# service squid restart
[root@squid ~]# iptables -F
[root@squid ~]# iptables -t nat -F
[root@squid ~]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[root@squid ~]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

4、web服务器配置
(1)安装Apache服务器

[root@web ~]# yum install httpd -y
[root@web ~]# systemctl start httpd

(2)设置默认路由

[root@web ~]# route add -net 192.168.10.0/24 gw 192.168.7.128

5、客户端测试
(1)不需要设置代理服务器,直接使用浏览器访问web网页
在这里插入图片描述
(2)在web服务器上打开日志文件,查看访问的IP地址

[root@web ~]# tail -f /var/log/httpd/access_log 
192.168.7.128 - - [18/Mar/2020:11:14:33 +0800] "GET / HTTP/1.1" 200 26 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
192.168.7.128 - - [18/Mar/2020:11:14:33 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://192.168.7.129/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
发布了95 篇原创文章 · 获赞 197 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_45682995/article/details/104934409
今日推荐