CDN基础实验——使用Nginx+squid实现代理缓存功能

一、实验要求

CDN基础实验——基于nginx+squid实现缓存功能 1、搭建两个web服务,使用nginx实现。
一个做源站(然后域名为:www.cdntest.com),配置实现功能:提供可以访问的web服务;另一个做反向代理,接收用户的请求,配置实现功能是:代理到后端的squid。
2、搭建一个squid服务,配置实现如下功能: (1)txt缓存1个月,jpg文件不缓存
(2)配置回源功能,源站为后端的web服务器(nginx)

二、实验环境

物理环境
虚拟操作系统        Windows 7
虚拟机             vmware12
centos镜像         centos-6.8
虚拟环境
nginx-proxy      192.168.127.16
squid          192.168.127.14
nginx-web      192.168.127.12

实验架构图
在这里插入图片描述

本次实验需要准备三台Linux(centos),一台作为源站(Nginx-proxy,IP:192.168.127.16),一台作为代理(Nginx-web,IP:192.168.127.12),一台作为缓存(squid,IP:192.168.127.14)

三、软件安装

3.1、安装Nginx_proxy(代理)

安装Nginx代理

[root@localhost /]# mkdir server
[root@localhost /]# cd /server/
[root@localhost server]# wget http://nginx.org/download/nginx-1.10.3.tar.gz
[root@localhost server]# tar -zxvf nginx-1.10.3.tar.gz
[root@localhost server]# cd nginx-1.10.3
[root@localhost nginx-1.10.3]#
[root@localhost nginx-1.10.3]# ./configure --prefix=/server/nginx
[root@localhost nginx-1.10.3]# make && make install
[root@localhost /]# vim /server/nginx/conf/nginx.conf

在Nginx代理的vim /server/nginx/conf/nginx.conf配置文件下添加如下的配置信息。
在这里插入图片描述

启动Nginx

[root@localhost /]# /server/nginx/sbin/nginx -t
nginx: the configuration file /server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /server/nginx/conf/nginx.conf test is successful
[root@localhost /]# /server/nginx/sbin/nginx -s reload

3.2、安装squid(缓存)

安装squid

[root@localhost /]# wget http://www.squid-cache.org/Versions/v3/3.1/squid-3.1.23.tar.gz
[root@localhost /]# tar -zxvf squid-3.1.23.tar.gz
[root@localhost /]# cd squid-3.1.23
[root@localhost squid-3.1.23]# ./configure --prefix=/squid
[root@localhost squid-3.1.23]# make && make install
[root@localhost /]# cd /squid/sbin/
[root@localhost sbin]# ./squid –z # 初始化squid缓存目录
[root@localhost sbin]# chmod 777 -R /squid/var/logs/ # 改变缓存目录的可写权限
[root@localhost sbin]# ./squid # 启动squid
[root@localhost /]# vim /squid/etc/squid.conf # 编辑squid配置文件

在squid的配置文件下编辑如下的配置信息。
在这里插入图片描述
编辑完配置信息之后可以用./squid -k parse检查配置是否正确。
启动squid

[root@localhost sbin]# ./squid -z
2019/12/10 22:35:39| Squid is already running!  Process ID 387
//如果启动报如下错误可以使用以下命令改变目录权限
[root@www sbin]# chmod -R 777 /var/log/squid/

如果启动squid报如下的错,用下面的命令改变目录得到权限即可

[root@www sbin]# ./squid WARNING: Cannot write log file:
/var/log/squid/cache.log /var/log/squid/cache.log: Permission denied
messages will be sent to ‘stderr’.

[root@www sbin]# chmod -R 777 /var/log/squid/

下面的命令是添加防火墙端口,并重启防火墙。

[root@localhost /]# vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
[root@localhost /]# /etc/init.d/network restart
[root@localhost sbin]# service iptables stop # 关闭防火墙
 [root@www sbin]# iptables -t nat -L –n
[root@www sbin]# /etc/init.d/iptables save
[root@www sbin]# /etc/init.d/iptables restart
[root@localhost sbin]# /etc/init.d/network start

3.3、安装Nginx-web(源站)

安装Nginx(源站 )

[root@localhost /]# mkdir server
[root@localhost /]# cd /server/
[root@localhost server]# wget http://nginx.org/download/nginx-1.10.3.tar.gz
[root@localhost server]# tar -zxvf nginx-1.10.3.tar.gz
[root@localhost server]# cd nginx-1.10.3
[root@localhost nginx-1.10.3]#
[root@localhost nginx-1.10.3]# ./configure --prefix=/server/nginx
[root@localhost nginx-1.10.3]# make && make install
[root@localhost /]# vim /server/nginx/conf/nginx.conf

编辑Nginx源站vim /server/nginx/conf/nginx.conf的配置文件,只需要将server_named的名称改成自己想要的域名即可。
在这里插入图片描述
注意:

在Nginx的源站的html目录下需要放置图片、txt文件及测试页面,方便测试(源站)

在这里插入图片描述
启动Nginx

[root@localhost /]# /server/nginx/sbin/nginx -t
nginx: the configuration file /server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /server/nginx/conf/nginx.conf test is successful
[root@localhost /]# /server/nginx/sbin/nginx -s reload

四、测试

在Nginx代理上的hosts里加入squid的IP地址
在这里插入图片描述
在squid上的hosts里加入Nginx代理的IP地址。
在这里插入图片描述
在Windows的C:\Windows\System32\drivers\etc\下的hosts文件里加入Nginx代理的IP地址,就能实现外网访问。在Windows通过浏览器输入www.cdntest.com/test.html访问页面
在这里插入图片描述
在这里插入图片描述
如果在Nginx代理上测试时出现如下错误,可能是端口不可达导致。我测试的是squid的IP地址,如果不通则添加防火墙访问规则。

[root@localhost /]# curl -I http://192.168.127.14/
curl: (7) couldn't connect to host

添加80端口访问规则

[root@localhost /]# iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT
//重启防火墙
[root@localhost /]# /etc/init.d/network restart

在这里插入图片描述
添加端口之后就能访问squid了

在代理Nginx上通过curl命令查看squid的缓存结果

[root@localhost /]# curl -I http://192.168.127.14/cdn.txt
//第一次访问html目录下的cdn.txt没有命中,MISS代表没有命中

在这里插入图片描述

第二次访问命中,表示第一次访问时进行了缓存

在这里插入图片描述
接下来访问squid的图片

[root@localhost /]# curl -I http://192.168.127.14/cdntest.jpg
//第一次访问没有缓存

在这里插入图片描述

第二次访问因为squid设置了图片不缓存,所以第二次访问图片没有缓存。
在这里插入图片描述

查看squid缓存日志

[root@localhost /]# cat /var/log/squid/cache.log
[root@localhost /]# head -10 /var/log/squid/cache.log

总结:本次的CDN基础实验通过反复的测试,虽然搞懂了原理,但配置实现却不是那么容易,这个实验的原理是利用Nginx的反向代理方式实现用户的访问,用户在浏览器里输入访问的域名,其实不是直接访问源站,用户也不知道源站的IP或域名信息,而是通过代理服务器Nginx实现访问源站,从而在一定程度上保证了源站的安全。同时用户不会直接访问源站,而是通过Nginx代理到squid,如果squid缓存有用户需要的内容,squid就不会再去源站寻找用户所需的内容,如果squid没有缓存用户所需的内容,squid则会去访问源站,从而在一定程度上减轻了源站的压力。

发布了56 篇原创文章 · 获赞 50 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43883625/article/details/103595829