Squid反向代理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Squid反向代理相关的知识,希望对你有一定的参考价值。

简介:
Squid反向代理:
技术分享图片
通过squid反向代理可以加速网站的访问速度,可以将不同的URL请求分发到后台不同的Web服务器上,同时互联网用户只能看到反向代理服务器的地址,加强了网站的访问安全。
反向代理原理:
Web 服务器加速(反向代理)是针对Web 服务器提供加速功能的。它作为代理Cache ,但并不针对浏览器用户,而针对一台或多台特定Web 服务器(这也是反向代理名称的由来)。实施反向代理,只要将Reverse Proxy Cache 设备放置在一台或多台Web 服务器前端即可。当互联网用户访问某个WEB 服务器时,通过DNS 服务器解析后的IP 地址是Reverse Proxy Server 的IP 地址, 而非原始Web 服务器的IP 地址, 这时Reverse Proxy Server 设备充当Web 服务器,浏览器可以与它连接,无需再直接与Web 服务器相连。因此,大量Web 服务工作量被卸载到反向代理服务上。不但能够防止外部网主机直接和web 服务器直接通信带来的安全隐患,而且能够很大程度上减轻web 服务器的负担,提高访问速度。
工作原理:
反向代理服务器位于本地WEB 服务器和Internet 之间,当用户浏览器发出一个HTTP 请求时,通过域名解析将请求定向到反向代理服务器(如果要实现多个WEB 服务器的反向代理,需要将多个WEB 服务器的域名都指向反向代理服务器)。由反向代理服务器处理器请求。反向代理一般只缓存可缓冲的数据(比如html 网页和图片等),而一些CGI 脚本程序或者ASP 之类的程序不缓存。它根据从WEB 服务器返回的HTTP 头标记来缓冲静态页面。有四个最重要HTTP 头标记:
#Last-Modified: 告诉反向代理页面什么时间被修改
#Expires: 告诉反向代理页面什么时间应该从缓冲区中删除
#Cache-Control: 告诉反向代理页面是否应该被缓冲
#Pragma: 告诉反向代理页面是否应该被缓冲

具体实验步骤:
-------------------------手工编译安装squid-------------------------------------
[[email protected] ~]#mkdir /abc
[[email protected] ~]#mount.cifs //192.168.100.10/rhel7 /abc
[[email protected] ~]#cd /abc/Y2C7/
[[email protected] Y2C]#tar zxvf squid-3.5.23.tar.gz -C /opt/
[[email protected] Y2C]#cd /opt/
[[email protected] opt]# yum install gcc gcc-c++ make -y
----------------------------配置-------------------------------------------------
[[email protected] opt]# cd squid-3.5.23/
[[email protected] 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

[[email protected] squid-3.5.23]# make && make install
[[email protected] squid-3.5.23]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
[[email protected] squid-3.5.23]# useradd -M -s /sbin/nologin squid
[[email protected] squid-3.5.23]# chown -R squid.squid /usr/local/squid/var/

[[email protected] squid-3.5.23]# vim /etc/squid.conf
#56行
http_access allow all #在http_access deny all上面插入
#61行
cache_effective_user squid #添加 指定程序用户
#62行
cache_effective_group squid #添加 指定账号基本组

[[email protected] squid-3.5.23]# squid -z #初始化缓存目录
[[email protected] squid-3.5.23]# squid #启动服务
[[email protected] squid-3.5.23]# netstat -ntap | grep 3128 #查看端口
tcp6 0 0 :::3128 :::* LISTEN 45271/(squid-1)

[[email protected] squid-3.5.23]# cd /etc/init.d/
[[email protected] 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

------------------------传统代理-------------------------------------------------

[[email protected] init.d]# chmod +x squid
[[email protected] init.d]# chkconfig --add squid
[[email protected] init.d]# chkconfig --level 35 squid on #开机自启动
[[email protected] init.d]# service squid stop #关闭
[[email protected] init.d]# service squid start #开启
[[email protected] init.d]# service squid check #检查语法

[[email protected] init.d]# vim /etc/squid.conf

61 cache_mem 64 MB #指定缓存功能所使用的内存空间大小,便于保持访问比较频繁的WEB
对象,容量最好为4的倍数,单位为MB,建议设为物理内存的1/4
62 reply_body_max_size 10 MB #允许用户下载的最大文件大小(默认为0,不进行限制)
63 maximum_object_size 4096 KB #允许保存到缓存空间的最大对象大小,以KB为单位,超过
大小限制的文件将不被缓存,而是直接转发给用户

[[email protected] init.d]# service squid restart
正在关闭 squid...
正在启动 squid...
[[email protected] init.d]# iptables -F
[[email protected] init.d]# setenforce 0
[[email protected] init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
----------------------------------------在第Web上操作-----------------------------------------------
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum install httpd -y
[[email protected] ~]# systemctl start httpd

------------------------------ACL访问控制--------------------------------
[[email protected] init.d]# vim /etc/squid.conf
第8行 acl hostlocal src 192.168.120.128/32
第38行 http_access deny hostlocal

[[email protected] init.d]# service squid stop
[[email protected] init.d]# service squid start
正在启动 squid...
[[email protected] init.d]# netstat -ntap | grep 3128
tcp6 0 0 :::3128 :::* LISTEN 45734/(squid-1)

技术分享图片

----------------------------------Squid日志分析------------------------------------
[[email protected] logs]# yum install gd gd-devel httpd -y
[[email protected] logs]# cd /abc/Y2C7/
[[email protected] Y2C7]# tar zxvf sarg-2.3.7.tar.gz -C /opt/
[[email protected] Y2C7]# cd /opt/
[[email protected] opt]# cd sarg-2.3.7/
[[email protected] sarg-2.3.7]# ./configure
--prefix=/usr/local/sarg
--sysconfdir=/etc/sarg
--enable-extraprotection #额外安全防护

#参数详解
prefix #指定配置文件安装路径
sysconfdir #配置文件目录
enable-extraprotection #安全保护功能

[[email protected] sarg-2.3.7]# make && make install

修改sarg配置文件,添加要监控的squid的日志信息等
[[email protected] sarg-2.3.7]# vim /etc/sarg/sarg.conf

#修改以下参数
access_log /usr/local/squid/var/logs/access.log #约第7行, 打开squid访问日志
title "Squid User Access Reports" #约第25行, 打开网页标题
output_dir /var/www/html/squid-reports #约第120行,sarg报告输出目录
user_ip no #约第178行,使用用户名显示,no代表禁用
exclude_hosts /usr/local/sarg/noreport #约第206行,指定不计入排序的站点列表文件
topuser_sort_field BYTES reverse #约第184行,在top排序中,指定连接次数、访问字节数,reverse代表降序
user_sort_field BYTES reverse #约第190行,对于用户访问记录,连接次数按降序排列
overwrite_report no #约第257行,当日期报告已经存在,是否覆盖报告
mail_utility mailx #约第289行,发送邮件报告的命令
charset UTF-8 #约第434行,使用字符集,utf-8
weekdays 0-6 #约第518行,指定top排序时的星期周期,0为周日
hours 7-12,14,16,18-20 #约第523行,指定top排序时的时间周期
www_document_root /var/www/html #约第633行,网页根目录

#添加不计入站点文件,添加的域名将不被显示在排序中
[[email protected] sarg-2.3.7]# touch /usr/local/sarg/noreport
#启动sarg并对观察squid的访问日志
[[email protected] sarg-2.3.7]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
[[email protected] sarg-2.3.7]# sarg
SARG: 纪录在文件: 90, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/squid-reports/2018Sep19-2018Sep19

[[email protected] html]# systemctl start httpd.service
[[email protected] html]# netstat -ntap | grep 80
tcp6 0 0 :::80 :::* LISTEN 49164/httpd
[[email protected] ~]# iptables -F
[[email protected] ~]# iptables -t nat -F

#客户机上访问squid服务器
http://192.168.120.135/squid-reports/
技术分享图片

#周期性计划任务执行每天生成报告
[[email protected] ~]# 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)
技术分享图片
---------------------------------------反向代理-----------------------------------------------------
squid 192.168.120.135

web1 accp 192.168.120.128
web2 benet 192.168.120.131

client 192.168.120.129

web1上
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# echo "this is accp" > index.html
[[email protected] html]# systemctl start httpd

web2上
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum install httpd -y
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# echo "this is benet" > index.html

squid上
[[email protected] ~]# vim /etc/squid.conf

#第60行
http_port 192.168.120.135:80 accel vhost vport
#第61行
cache_peer 192.168.120.128 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
#第62行
cache_peer 192.168.120.131 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
#第63行
cache_peer_domain web1 web2 www.yun.com

[[email protected] ~]# service squid stop
[[email protected] ~]# netstat -ntap | grep 80
tcp6 0 0 :::80 ::: LISTEN 2534/httpd
[[email protected] ~]# systemctl stop httpd.service
[[email protected] ~]# service squid start
正在启动 squid...
[[email protected] ~]# netstat -ntap | grep 80
tcp 0 0 192.168.120.135:80 0.0.0.0:
LISTEN 54412/(squid-1)

#客户机上访问
切换到管理员建立本地解析C:WindowsSystem32driversetc
技术分享图片
技术分享图片
http://www.yun.com
技术分享图片

以上是关于Squid反向代理的主要内容,如果未能解决你的问题,请参考以下文章

Squid代理服务---反向代理

squid详解(正向代理透明代理反向代理)

实践出真知——基于squid实现反向代理实践

squid代理服务器(传统代理透明代理反向代理ACL日志分析)

利用 squid 反向代理提高网站性能

squid正向代理反向代理