linux项目实战---lamp架构(九)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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值