Squid 代理服务器Varnish服务器

Posted

tags:

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

1 案例1:反向代理
squid 功能全,老(文档)
正向代理,反向代理,透明代理,但是性能差(缓存放在/var/spool/squid硬盘),用硬盘放缓存太慢
varnish 新,性能强(缓存放在内存,速度快),轻量级软件(秒开)
ps 200k ps1G 只能做反向代理

通过配置代理服务器,实现以下目标:
1.代理服务器可以将远程的Web服务器页面缓存在本地
2.代理服务器端口设置为80端口
3.用户通过访问代理服务器即可获得远程Web服务器上的页面内容
4.远程Web服务器对客户端用户是透明的
5.利用缓存机制提高网站的响应速度
1.2 方案
使用3台RHEL7虚拟机,其中一台作为Squid代理服务器,该服务器用来连接两个网段,因此需要配置两块网卡,地址分别为 192.168.4.5和192.168.2.5。一台作为客户端测试主机,IP地址为192.168.4.100。一台Web服务器,地址为 192.168.2.100,该Web服务器为其他代理提供Web数据源。
实验环境所需要的主机及对应的IP设置列表如表-1所示。
表-1 主机列表

实验拓扑如图-1所示。

图-1
1.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:构建web服务器
1)使用yum安装web软件包
1.[[email protected] ~]# yum -y install httpd
2... ..
3.[[email protected] ~]# rpm -q httpd
4.httpd-2.4.6-40.el7.x86_64
2)启用httpd服务,并设为开机自动运行
1.[[email protected] ~]# systemctl start httpd ; systemctl enable httpd
httpd服务默认通过TCP 80端口监听客户端请求:
1.[[email protected] ~]# netstat -anptu | grep httpd
2.tcp????????0????????0????????:::80????????:::????????LISTEN????????2813/httpd
3)为Web访问建立测试文件
在网站根目录/var/www/html下创建一个名为index.html的首页文件:
1.[[email protected] ~]# cat /var/www/html/index.html
2.<html>
3.<title>Welcome</title>
4.<body>
5.<h1>192.168.2.100</h1>
6.</body>
7.</html>
步骤二:部署Squid代理服务器
1)使用yum安装squid软件包:
1.[[email protected] ~]# yum -y install squid
2... ..
2)修改/etc/squid/squid.conf配置文件:
1.[[email protected] ~]# vim /etc/squid/squid.conf
2... ..
3.http_port 80 vhost????????????????????????????//设置反向代理
4.visible_hostname svr5.tarena.com????????????????//设置主机名,默认没有该语句
5.cache_peer 192.168.2.100 parent 80 0 originserver //定义后端真实服务器信息
6.cache_dir ufs /var/spool/squid 200 16 256????????//硬盘缓存,缓存容量为200M,自动创建16个一级子目录和256个二级子目录
7.http_access allow all???????????????????????? //允许本机所有主机使用代理服务器
3)启动squid服务,并设置为开机启动:
1.[[email protected] ~]# systemctl start squid; systemctl enable squid
4)squid服务通过TCP 80端口监听客户端请求:
1.[[email protected] ~]# netstat -anptu | grep 80
2.tcp????????0????????0????????:::80????????:::
????????LISTEN????????3213/(squid)
步骤三:客户端测试
2)客户端开启浏览器访问
1.[[email protected] ~]# curl http://192.168.4.5????????????//返回的是2.100服务的页面

2 案例2:使用Varnish加速Web
/etc/sysconfig/varnish 前端配置
VARNISH_LISTEN_PORT=80
#

Telnet admin interface listen address and port

VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082 用户端口号,管理员专用
#

Shared secret file for admin interface

VARNISH_SECRET_FILE=/etc/varnish/secret
#

The minimum number of worker threads to start

VARNISH_MIN_THREADS=50 最少启动50个线程 (动态调整)
#

The Maximum number of worker threads to start

VARNISH_MAX_THREADS=1000 最多启动1000个线程 (处理并发量多,但是消耗内存)

Cache file location

VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin
#

Cache file size: in bytes, optionally using k / M / G / T suffix,

or in percentage of available disk space using the % suffix.

VARNISH_STORAGE_SIZE=1G

Backend storage specification

VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}" 用文件缓存

/etc/varnish/default.vcl 后端配置

程序program 安装的QQ(在硬盘里)
进程process 双击QQ启动进程(内存里)多进程
线程thread 干活的,依附于进程
2.1 问题
通过配置Varnish缓存服务器,实现如下目标:
?使用Varnish加速后端Apache Web服务
?使用varnishadm命令管理缓存页面
?使用varnishstat命令查看Varnish状态
2.2 方案
通过源码编译安装Varnish缓存服务器
?编译安装Varnish软件
?复制启动脚本与配置文件
修改配置文件,缓存代理源Web服务器,实现Web加速功能
使用3台RHEL7虚拟机,其中一台作为Web服务器(192.168.2.100)、一台作为Varnish代理服务器(192.168.4.5,192.168.2.5),另外一台作为测试用的Linux客户机(192.168.2.100),如图-2所示。

图-2
对于Web服务器的部署,此实验中仅需要安装httpd软件、启动服务,并生成测试首页文件即可,默认httpd网站根路径为/var/www/html,首页文档名称为index.html。
2.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:构建Web服务器
1)使用yum安装web软件包
1.[[email protected] ~]# yum -y install httpd
2)启用httpd服务,并设为开机自动运行
1.[[email protected] ~]# systemctl start httpd ; systemctl enable httpd
httpd服务默认通过TCP 80端口监听客户端请求:
1.[[email protected] ~]# netstat -anptu | grep httpd
2.tcp????????0????????0????????:::80????????:::????????LISTEN????????2813/httpd
3)为Web访问建立测试文件
在网站根目录/var/www/html下创建一个名为index.html的首页文件:
1.[[email protected] ~]# cat /var/www/html/index.html
2.<html>
3.<title>Welcome</title>
4.<body>
5.<h1>192.168.2.100</h1>
6.</body>
7.</html>
步骤二:部署Varnish缓存服务器
1)编译安装软件
1.[[email protected] ~]# yum -y install gcc readline-devel pcre-devel????//安装软件依赖包
2.[[email protected] ~]# useradd -s /sbin/nologin varnish????????????????//创建账户
3.[[email protected] ~]# tar -xzf varnish-3.0.6.tar.gz
4.[[email protected] ~]# cd varnish-3.0.6
5.[[email protected] varnish-3.0.6]# ./configure --prefix=/usr/local/varnish
6.[[email protected] varnish-3.0.6]# make && make install
2)复制启动脚本及配置文件
1.[[email protected] varnish-3.0.6]# cp redhat/varnish.initrc /etc/init.d/varnish
2.[[email protected] varnish-3.0.6]# cp redhat/varnish.sysconfig /etc/sysconfig/varnish
3.[[email protected] varnish-3.0.6]# ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/
4.[[email protected] varnish-3.0.6]# ln -s /usr/local/varnish/bin/
/usr/bin/
3)修改Varnish文件
1.[[email protected] ~]# vim /etc/sysconfig/varnish
2.66行:VARNISH_LISTEN_PORT=80????????????????????????????????#默认端口
3.89行:VARNISH_STORAGE_SIZE=64M????????????????????????????????#定义缓存大小
4.92行:VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"? ? ????#基于内存方式缓存
5.[[email protected] ~]# /etc/init.d/varnish start 开启varnish(不受systemctl控制)红帽6控制
6.Reloading systemd: [ 确定 ]Starting varnish (via systemctl):
直接ctrl c,查看netstat,已经启动varnish
4)修改代理配置文件
1.[[email protected] ~]# mkdir /etc/varnish
2.[[email protected] ~]# cp /usr/local/varnish/etc/default.vcl /etc/varnish/
3.[[email protected] ~]# uuidgen > /etc/varnish/secret
4.[[email protected] ~]# vim /etc/varnish/default.vcl
5.backend default {

  1. .host = "192.168.2.100";
  2. .port = "80";
  3. }
    9.[[email protected] ~]# service varnish start 红帽7支持。
    查看日志
    varnishlog (ping varnish 的后台看后台还在不在)
    varnishncsa 访问日志(有人访问会出一条日志)

varnish缓存过期问题(缓存在内存,删不掉)
1.等,自动更新,自己同步数据
2.手动更新 varnishadm 命令清理缓存
在web1上生成一个页面echo “c” > /var/www/html/c.html
在客户端测试curl http://192.168.4.1[缓存]
在web1修改页面
echo “xx” > /var/www/html/c.html
客户端测试curl http://192.168.4.1[过期]
在proxy: varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082

ban.url c.html
在客户端测试: curl http://192.168.4.1[新的数据]

步骤三:客户端测试
1)客户端开启浏览器访问
1.[[email protected] ~]# curl http://192.168.4.5
步骤四:其他操作
1)查看varnish日志
1.[[email protected] ~]# varnishlog????????????????????//varnish日志
2.[[email protected] ~]# varnishncsa????????????????????//访问日志
2)更新缓存数据,在后台web服务器更新页面内容后,用户访问代理服务器看到的还是之前的数据,说明缓存中的数据过期了需要更新(默认也会自动更新,但非实时更新)。
1.[[email protected] ~]# varnishadm –S /etc/varnish/secret –T 127.0.0.1:6082 ban.url 页面文件名
2.//清空缓存数据,支持正则表达式
清除缓存内容:
Varnishadm -T IP:Port -S securefile ban.url
Varnishadm -s /etc/varnish/secret ban.url/

代理:
yum -y install squid
31 vim /etc/squid/squid.conf
32 systemctl restart squid
33 systemctl enable squid
34 netstat -anptu | grep 80
35 yum -y install gcc readline-devel pcre-devel
36 firewall-cmd --set-default-zone=trusted
37 tail /var/log/squid/access.log
38 cd /var/spool/squid/00/00
39 ls
40 tailf /var/log/squid/access.log

42 pwd
43 ls
44 systemctl stop squid
45 ls /
46 ls /root
47 cd..
48 cd /root
49 tar -xf lnmp_soft-2017-03-28.tar.gz
50 ls
51 cd lnmp_soft/
52 ls
53 sh install_lnmp.sh
56 vim /etc/varnish/default.vcl
57 service varnish start
58 systemctl stop httpd 80
59 systemctl stop squid 80
60 /etc/init.d/varnish start
61 netstat -nutlp | grep
62 netstat -nutlp | grep 80
63 curl http://192.168.4.5
64 curl http://192.168.2.100
65 curl http://192.168.4.100
66 cd ..
67 varnishlog
68 varnishncsa
69 cd squid
70 cd /etc/squid
71 varnishncsa
72 varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
73 cd
74 vim /etc/squid/squid.conf
Squid服务器
软件包:squid-3.3.8-26.....
系统服务: squid
主程序:/usr/sbin/squid
主配置文件:/etc/squid/squid.conf
默认监听端口:TCP3128
默认访问日志:/var/log/squid/s=access.log

配置文件::
http_port 80 vhost //设置反向代理
visible_hostname svr5.tarena.com //设置主机名(随意写)
cache_peer 192.168.2.100 parent 80 0 originserver //定义后端真实服务器信息
cache_dir ufs /var/spool/squid 200 16 256 //硬盘缓存,缓存容量为200M,自动创建16个一级子目录和256个二级子目录
http_access allow all //允许本机所有主机使用代理服务器

Varnish服务器
/etc/varnish 配置文件目录
/etc/init.d/varnish varmish的启动程序
/etc/sysconfig/varnish 配置文件,varnish定义自身属性
/etc/varnish/default.vcl 默认配置文件,定义后段节点的
/usr/bin/varnishadm 客户端程序
/usr/bin/varnishstat 状态监控

tailf /var/log/secure 动态查看varnish后10行日志

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

Squid代理服务器 Varnish服务器

Linux Operation学习------Squid/Varnish

Squid服务器,Varnish服务器

代理服务squid和varnish使用与区别

高性能反向代理服务器Varnish安装与使用

varnish和squid的对比