Squid服务器应用

Web daili的工作机制

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

daili的基本类型

1、传统daili:适用于Internet,需明确指定服务端
2、透明daili:客户机不需指定daili服务器的地址和端口,而是通过默认路由、防火墙策略将Web访问重定向给daili服务器处理
使用daili的好处
1、提高Web访问速度
2、隐藏客户机的真实IP地址
实验准备环境
squid服务器 主机名:squid 20.0.0.10
web服务器 主机名:web 20.0.0.20
客户机服务器 主机名:kehuji 20.0.0.30

一、搭建squid服务

1、导入squid数据包

[root@squid opt]# ll
-rw-r--r--  1 root root   squid-3.5.23.tar.gz

2、解压数据包

[root@squid opt]# tar zxvf squid-3.5.23.tar.gz

3、进入目录配置安装目录、功能模块

[root@squid opt]# cd squid-3.5.23/
./configure --prefix=/usr/local/squid \
--sysconfdir=/etc \
--enable-arp-acl \
--enable-linux-netfilter \
--enable-linux-tproxy \
--enable-async-io=100 \
--enable-err-language="Simplify_Chinese" \
--enable-underscore \
--enable-poll \
--enable-gnuregex

4、编译安装

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

5、优化路径

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

6、创建一个程序用户

[root@squid squid-3.5.23]# useradd -M -s /sbin/nologin squid

7、设置属主、属组

[root@squid squid-3.5.23]# chown -R squid.squid /usr/local/squid/var/

8、修改配置文件

[root@squid squid-3.5.23]# vim /etc/squid.conf
cache_effective_user squid        #添加   指定程序用户
cache_effective_group squid       #添加   指定账号基本组
coredump_dir /usr/local/squid/var/cache/squid

9、设置启动管理

[root@squid squid-3.5.23]# 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..."
   ;;
   reload)
      $CMD -k reconfigure
   ;;
   check)
      $CMD -k parse
   ;;
   *)
      echo "用法:$0{start|stop|status|reload|check|restart}"
   ;;
esac

10、设置权限

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

11、启动squid服务

[root@squid init.d]# systemctl start squid

二、搭建web服务

1、安装web服务

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

2、设置web服务主页

[root@web ~]# echo "<h1>This is web</h1>" > /var/www/html/index.html

3、启动web服务

[root@web ~]# systemctl start httpd.service

三、设置传统daili

1、修改daili服务器配置文件

[root@squid init.d]# vim /etc/squid.conf
在    # And finally deny all other access to this proxy下面添加
http_access allow all

在# Squid normally listens to port 3128下面添加
cache_mem 64 MB                   #指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最好为4的倍数,单位为MB,建议设为物理内存的1/4
reply_body_max_size 10 MB         #允许用户下载的最大文件大小,以字节为单位。默认设置0表示不进行限制
maximum_object_size 4096 KB       #允许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,而是直接转发给用户

2、重启squid服务器

[root@squid init.d]# systemctl restart squid.service

四、测试传统daili

1、客户机在浏览器设置daili
2、输入地址测试
在这里插入图片描述

五、设置透明daili

squid服务器 主机名:squid 内网卡:20.0.0.10外网卡:192.168.0.103
web服务器 主机名:web 20.0.0.20
客户机服务器 主机名:kehuji ip地址:192.168.0.104网关:192.168.0.103
1、在squid服务器上添加一张网卡(此网卡用于公网传输)
2、重启网卡
[root@squid ~]# systemctl restart network
3、查看网卡信息
[root@squid ~]# ifconfig
在这里插入图片描述
4、开启路由功能

[root@squid ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1		#增加

5、在web服务器添加一条路由

[root@web ~]# route add -net 192.168.0.0/24 gw 20.0.0.10

6、修改squid配置文件设置透明daili

[root@squid ~]# vim /etc/squid.conf
将   http_port 3128       修改为  http_port 192.168.0.103:3128 transparent

7、设置入站规则

[root@squid ~]# iptables -F
[root@squid ~]# iptables -t nat -F
[root@squid ~]# iptables -t nat -I PREROUTING -i ens37 -s 192.168.0.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[root@squid ~]# iptables -t nat -I PREROUTING -i ens37 -s 192.168.0.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

客户机测试
在这里插入图片描述
查看web服务器访问日志
在这里插入图片描述

六、设置acl访问控制列表

1、禁止客户机访问

[root@squid ~]# vim /etc/squid.conf
在   acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines下面添加一下内容
acl host     src 192.168.0.104/32

在  # Only allow cachemgr access from localhost下面加
http_access deny host

2、重启squid服务

[root@squid ~]# systemctl restart squid.service

3、测试
在这里插入图片描述

七、squid日志分析

1、安装依赖环境

[root@squid ~]# yum -y install gd

2、导入sarg数据包
3、创建安装目录

[root@squid opt]# mkdir /usr/local/sarg

4、解压编译安装

[root@squid opt]# tar zxvf sarg-2.3.7.tar.gz
[root@squid sarg-2.3.7]# make && make install

5、修改配置文件

[root@squid sarg-2.3.7]# cd /etc/sarg/
[root@squid sarg]# vim sarg.conf 
#access_log /usr/local/squid/var/logs/access.log 			去掉#
#title "Squid User Access Reports"			                去掉#
#output_dir /var/www/html/squid-reports						去掉#
#user_ip no													去掉#
#exclude_hosts none  										修改为       exclude_hosts /usr/local/sarg/noreport
#topuser_sort_field BYTES reverse							修改为	topuser_sort_field connect reverse
#overwrite_report no										去掉#
#mail_utility mailx											修改为		mail_utility mailq.postfix
#charset Latin1												修改为		charset UTF-8
#weekdays 0-6												去掉#
#hours 0-23													去掉#
#www_document_root /var/www/html							去掉#

6、创建一个文件

[root@squid sarg]# touch /usr/local/sarg/noreport

7、优化路径

[root@squid sarg]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/

8、启动服务

[root@squid ~]# sarg 

9、安装httpd服务

[root@squid ~]# yum -y install httpd

10、启动httpd服务

[root@squid ~]# systemctl start httpd.service

11、查看报告
在这里插入图片描述
12、周期性计划任务执行每天生成报告crontab

[root@squid ~]# sarg -l /usr/local/squid/var/logs/access.log -o /var/www/html/squid-reports/ -z -d $(date -d "1 day ago" +%d/%m/%Y)-$(date +%d/%m/%Y)
[root@squid ~]# crontab -e
30 22 * * * sarg -l /usr/local/squid/var/logs/access.log -o /var/www/html/squid-reports/ -z -d $(date -d "1day ago" +%d/%m/%Y)-$(date +%d/%m/%Y)

八、反向daili

安装两个web服务器
1、web1

[root@web1 ~]#  yum -y install httpd
[root@web1 ~]# echo "<h1>This is web1</h1>" > /var/www/html/index.html
[root@web1 ~]# systemctl start httpd
[root@Web1 ~]# route add -net 192.168.0.0/24 gw 20.0.0.10

2、web2

[root@web2 ~]#  yum -y install httpd
[root@web2 ~]# echo "<h1>This is web2</h1>" > /var/www/html/index.html
[root@web2 ~]# systemctl start httpd
[root@Web2 ~]# route add -net 192.168.0.0/24 gw 20.0.0.10

3、配置squid服务器

[root@squid ~]# iptables -F
[root@squid ~]# iptables -t nat -F
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@squid ~]# vim /etc/squid.conf
在   # Squid normally listens to port 3128下面设置如下内容
http_port 20.0.0.10:80 accel vhost vportcache_peer 20.0.0.20 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1cache_peer 20.0.0.40 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
cache_peer_domain web1 web2 www.abc.com

[root@squid ~]# systemctl restart squid.service

4、客户机测试

[root@kehuji ~]# vim /etc/hosts
20.0.0.10     www.abc.com		#增加

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值