了解Squid代理服务器应用

 一、缓存代理概述

Web代理的工作机制

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

 

代理的基本类型

·传统代理:适用于lnternet,需明确指定服务端

·透明代理:客户机不需指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将Web访问重定向给代理服务器处理

使用代理的好处

·提高Web访问速度

·隐藏客户机的真实IP地址

 

二、squid主要组成部分

 

三、Squid各种代理的定义

1、传统代理

环境

 (1)、Squid服务器配置

①安装依赖环境

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

②编译安装squid服务

[root@squid ~]# tar zxf squid-3.5.23.tar.gz -C /opt
[root@squid ~]# cd /opt/squid-3.5.23/
[root@squid 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
[root@squid squid-3.5.23]# make && make install

③优化路径

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

④修改配置文件,优化启动项

[root@squid ~]# vi /etc/squid.conf
cache_effective_user squid       
cache_effective_group squid     
[root@squid ~]# squid -k parse 
[root@squid ~]# squid -z 
[root@squid ~]# squid 
[root@squid ~]# netstat -anpt | grep squid
tcp6       0      0 :::3128                 :::*                    LISTEN      104314/(squid-1)
⑤添加服务到service管理
[root@squid ~]# vi /etc/init.d/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
[root@squid ~]# chmod +x /etc/init.d/squid
[root@squid ~]# chkconfig --add squid
[root@squid ~]# chkconfig --level 35 squid on

⑥配置传统代理

[root@squid ~]# vi /etc/squid.conf
# And finally deny all other access to this proxy
http_access allow all           
http_access deny all
# Squid normally listens to port 3128
http_port 3128
cache_mem 64 MB            
reply_body_max_size 10 MB     
maximum_object_size 4096 KB     发给用户
[root@squid ~]# setenforce 0
[root@squid ~]# iptables -F
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@squid ~]# systemctl restart squid

 

(2)、Web服务器配置

安装httpd,并设置默认网页内容

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

[root@web ~]# cd /var/www/html/
[root@web html]# vi index.html
<h1>this is a web!!!</h1>
[root@web html]# systemctl restart httpd
[root@web html]# netstat -anpt | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      3432/httpd
[root@web html]# curl http://localhost
<h1>this is web!!!</h1>

 

(3)、客户机测试

 

 

(4)、查看日志文件,看访问的IP

[root@web html]# cat /var/log/httpd/access_log
20.0.0.30 - - [09/Nov/2020:14:42:40 +0800] "GET / HTTP/1.1" 200 24 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
20.0.0.30 - - [09/Nov/2020:14:42:40 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
20.0.0.30 - - [09/Nov/2020:14:42:40 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"

是客户机的IP

 

(5)、再网页上进行代理配置并测试

 

 

 

 

 

(6)、查看日志文件,看访问的IP

[root@web html]# cat /var/log/httpd/access_log
20.0.0.10 - - [09/Nov/2020:14:54:52 +0800] "GET / HTTP/1.1" 200 24 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
20.0.0.10 - - [09/Nov/2020:14:54:53 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"

成功变成Squid服务器的IP

 

2、透明代理

在搭建的传统代理基础上做如下修改:

①squid服务器添加一块网卡:192.168.100.10(仅主机模式);开启路由转发功能,开启透明代理;配置防火墙规则;

②web服务器不变;

③客户端IP地址修改为192.168.100.20,且浏览器关闭手动代理设置

(1)、Squid服务器配置

①开启路由功能

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

②修改配置文件

[root@squid ~]# vi /etc/squid.conf
http_port 192.168.100.10:3128 transparent
[root@squid ~]# systemctl restart squid.service
[root@squid squid-3.5.23]# netstat -anpt | grep squid
tcp        0      0 192.168.100.10:3128     0.0.0.0:*               LISTEN      114617/(squid-1)   

③设置防火墙规则

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

(2)、web端设置

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

(3)、客户机测试(客户机网关要设置成Squid内网网关IP)

①网页修改为不使用代理

 

 

 

 ②访问并查看日志

 

 

[root@web html]# cat /var/log/httpd/access_log
20.0.0.10 - - [09/Nov/2020:15:52:02 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
20.0.0.10 - - [09/Nov/2020:16:06:40 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"

 

四、ACL控制

使用acl访问控制列表,禁止客户机访问web服务器

1、修改配置文件

[root@squid squid-3.5.23]# vi /etc/squid.conf
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines
acl host     src 192.168.100.20/32    
# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny host             
http_access deny manager
[root@squid squid-3.5.23]# systemctl restart squid
[root@squid squid-3.5.23]# netstat -anpt | grep squid
tcp        0      0 192.168.100.10:3128     0.0.0.0:*               LISTEN      115270/(squid-1)

2、测试

 

 

五、Squid日志分析

1、安装依赖环境

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

2、编译安装日志分析软件

[root@squid ~]# mkdir /usr/local/sarg
[root@squid ~]# tar zxf sarg-2.3.7.tar.gz -C /opt
[root@squid ~]# cd /opt/sarg-2.3.7/
[root@squid sarg-2.3.7]# ./configure \
> --prefix=/usr/local/sarg \
> --sysconfdir=/etc/sarg \  
> --enable-extraprotection  
[root@squid sarg-2.3.7]# make && make install

3、修改配置文件

[root@squid ~]# vi /etc/sarg/sarg.conf
7/ access_log /usr/local/squid/var/logs/access.log    
25/ title "Squid User Access Reports"     
120/ output_dir /var/www/html/squid-reports    
178/ user_ip no       
206/ exclude_hosts /usr/local/sarg/noreport   
184/ topuser_sort_field connect reverse   
(注释掉)190/ #  user_sort_field reverse   
257/ overwrite_report no  
289/ mail_utility mailq.postfix   
434/ charset UTF-8   
518/ weekdays 0-6 
525/ hours 0-23  
633/ www_document_root /var/www/html  

4、添加不计入站点文件,添加的域名将不被显示在排序中

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

5、优化启动项并启动服务

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

[root@squid sarg-2.3.7]# sarg
SARG: 纪录在文件: 214, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/squid-reports/2020Nov09-2020Nov09

6、安装启动httpd服务

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

7、查看报告

 

 8、做周期性计划任务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)

SARG: TAG: access_log /usr/local/squid/var/logs/access.log
SARG: TAG: title "Squid User Access Reports"
SARG: TAG: output_dir /var/www/html/squid-reports
SARG: TAG: user_ip no
SARG: TAG: topuser_sort_field BYTES reverse
SARG: TAG: exclude_hosts /usr/local/sarg/noreport
SARG: TAG: overwrite_report no
SARG: TAG: mail_utility mailq.postfix
SARG: TAG: charset UTF-8
SARG: TAG: weekdays 0-6
SARG: TAG: hours 0-23
SARG: TAG: www_document_root /var/www/html
SARG: 纪录在文件: 127, reading: 100.00%
SARG: 期间被日志文件覆盖: 07/11/2020 - 08/11/2020
SARG: (info) date=08/11/2020
SARG: (info) period=2020 11月 07-2020 11月 08
SARG: (info) outdirname=/var/www/html/squid-reports//2020Nov07-2020Nov08
SARG: (info) Dansguardian report not produced because no dansguardian configuration file was provided
SARG: (info) No redirector logs provided to produce that kind of report
SARG: (info) No downloaded files to report
SARG: (info) Authentication failures report not produced because it is empty
SARG: (info) Redirector report not generated because it is empty
SARG: 成功的生成报告在 /var/www/html/squid-reports//2020Nov07-2020Nov08
[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 "1
day ago" +%d/%m/%Y)-$(date +%d/%m/%Y)
测试在浏览器输入 20.0.0.10/squid-reports/,又会出现一条新的访问记录,然后查看/var/www/html/squid-reports文件
[root@squid ~]# cd /var/www/html/squid-reports/
[root@squid squid-reports]# ll
drwxr-xr-x. 5 root root  213 11月  9 17:34 2020Nov08-2020Nov09
drwxr-xr-x. 5 root root  213 11月  9 17:10 2020Nov09-2020Nov09
drwxr-xr-x. 2 root root   92 11月  9 17:10 images
-rw-r--r--. 1 root root 4686 11月  9 17:34 index.html

 

六、Squid反向代理

在透明模式的基础上进行反向代理

因为httpd会占用80端口,所以必须关闭squid服务器中的httpd服务

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 ~]# netstat -anpt | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      50552/httpd
[root@web1 ~]# route add -net 192.168.100.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 ~]# netstat -anpt | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      50552/httpd
[root@web2 ~]# route add -net 192.168.100.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 ~]# vi /etc/squid.conf
# Squid normally listens to port 3128
http_port 20.0.0.10:80 accel vhost vport ###squid外网口IP
cache_peer 20.0.0.20 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_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.xuhao.com
[root@squid ~]# systemctl restart squid

4、测试

 

 

 

posted @ 2020-11-16 23:19  Biu小怪兽  阅读(170)  评论(0编辑  收藏  举报