CDN内容分发网络
一.CDN简介
CDN的全称是Content Delivery Network,即内容分发网络。CDN是构建在现有网络基础之上的智能虚拟网络,依靠部署在各地的边缘服务器,通过中心平台的负载均衡、内容分发、调度等功能模块,使用户就近获取所需内容,降低网络拥塞,提高用户访问响应速度和命中率。CDN的关键技术主要有内容存储和分发技术。
二.CDN部署
1.安装varnish
yum install -y *
查看varnish服务信息:
vim /usr/lib/systemd/system/varnish.service
sysctl -a | grep file
修改限制:
vim /etc/security/limits.conf
netstat -antlp
修改配置文件:
vim /etc/varnish/default.vcl
#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
# Default backend definition. Set this to point to your content server.
# 定义web1访问172.25.9.2的80端口,web2为访问172.25.9.3的80端口
backend web1 {
.host = "172.25.3.2";
.port = "80";
}
backend web2 {
.host = "172.25.3.3";
.port = "80";
}
#当访问域名为www.westos.org/westos.org 访问web1;当访问域名为bbs.westos.org访问web2;其余返回404
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend_hint = web1;
#return(pass);
}
elseif (req.http.host ~ "^bbs.westos.org") {
set req.backend_hint = web2;
#return(pass);
}
else {
return(synth(404,"Not in cache"));
}
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
}
sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.
}
# 如果命中,显示HIT from westos cache;否则显示MISS from westos cache
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
设定监听端口:
vim varnish.params
启动服务:
systemctl start varnish
netstat -antlp
2.server2/3安装apache
server2:
yum install -y httpd
echo 'www.westos.org' > /var/www/html/index.html
systemctl start httpd
server3:
yum install -y httpd
echo 'bbs.westos.org' > /var/www/html/index.html
systemctl start httpd
3.测试
真机配置本地解析:
访问测试:
cache缓存:
第一次访问没有缓存
第二次访问直接加载缓存:
缓存保存时间,默认120s
三.varnish+nginx负载均衡
重新打开全新的虚拟机server4配置nginx
server4
解压tar包并进入解压后目录
tar zxf nginx-1.20.1.tar.gz
ls
cd nginx-1.20.1/
安装依赖性
yum install -y gcc
yum install -y pcre-devel
yum install -y openssl-devel
编译安装
./configure --help
./configure --with-http_ssl_module
make
make install
制作软链接
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
编辑nginx配置文件进行负载均衡
vim /usr/local/nginx/conf/nginx.conf
检测语法,开启nginx服务
nginx -t
nginx
server1
将cdn与nginx连接完成负载均衡
vim default.vcl
# 添加web3
backend web3 {
.host = "172.25.3.4";
.port = "80";
}
...
# 访问域名www.westos.org,代理到web3
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend_hint = web3;
return(pass);
}
elseif (req.http.host ~ "^bbs.westos.org") {
set req.backend_hint = web2;
#return(pass);
}
else {
return(synth(404,"Not in cache"));
}
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
}
重载varnish:
systemctl reload varnish.service
测试:
查看日志:
cat /var/log/httpd/access_log
四.获取访问端ip
在负载均衡出注释server3,只观察server2
server2
编译nginx,红框为添加real ip
模块:
./configure --with-http_realip_module --with-http_ssl_module
make
make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
关闭apche,启动nginx:
systemctl stop httpd
nginx
添加real ip模块
vim /usr/local/nginx/conf/nginx.conf
real_ip_header X-Forwarded-For;
real_ip_recursive on;
set_real_ip_from 172.25.9.0/24;
重载nginx
nginx -s reload
测试:
curl www.westos.org
查看日志: 172.25.3.250
为真实ip
五.varnish健康监测
probe backend_healthcheck {
.url = "/index.html";
.window = 3;
.threshold = 2;
.interval = 3s;
}
backend web1 {
.host = "172.25.3.2";
.port = "80";
.probe = backend_healthcheck;
}
backend web2 {
.host = "172.25.3.3";
.port = "80";
.probe = backend_healthcheck;
}