CDN--- Nginx(负载)+varish(缓存)+web

CDN的介绍

CDN是一组分布在多个不同的地理位置的WEB服务器,用于更加有效的向用户发布内容,在优化性能时,会根据距离的远近来选择

CDN系统能实时的根据网络流量和各节点的连接,负载状况及用户的距离和响应时间等综合信息将用户的请求重新导向离用户最近的服务节点上,其目的是使用户能就近的获取请求数据,解决网络拥堵,提高访问速度,解决由于网络带宽小,用户访问量大,网点分布不均等原因导致的访问速度慢的问题。

由于CDN部署在网络运营商的机房,这些运营商又是终端用户网络的提供商,因此用户请求的第一跳就到达CDN服务器,当CDN服务器中缓存有用户请求的数据时,就可以从CDN直接返回给浏览器,因此就可以提高访问速度eal_ip_header   

varnish简介

varnish是一个开源的反向代理软件和HTTP加速器,是一个新贵的缓存软件,与缓存的元老squid相比,varnish更轻量级一些,varnish具有性能更高、速度更快、管理更方便。

一、实验环境

三台虚拟机
server_lue3:172.25.6.5
server_lue4:172.25.6.6
server_lue5:172.25.6.7
一台真机  172.25.6.250

二、varnish的下载及配置

  • varnish的安装

1.阿里巴巴开源镜像站下载 网址:http://mirrors.aliyun.com/
varnish-4.0.5-1.el7.x86_64.rpm
varnish-libs-4.0.5-1.el7.x86_64.rpm
jemalloc-3.6.0-1.el7.x86_64.rpm
jemalloc-devel-3.6.0-1.el7.x86_64.rpm
2.安装
[root@lue3 ~]# yum install -y varnish-4.0.5-1.el7.x86_64.rpm varnish-libs-4.0.5-1.el7.x86_64.rpm jemalloc-3.6.0-1.el7.x86_64.rpm jemalloc-devel-3.6.0-1.el7.x86_64.rpm
3.开启varnish
[root@lue3 ~]# systmectl start varnish
  • varnish的配置

1.编辑/etc/varnish/default.vcl 配置后端服务器
[root@lue3 varnish]# vim /etc/varnish/default.vcl   
 16 backend default {
 17     .host = "172.25.6.250";
 18     .port = "80";
 19 }
2.编辑/etc/sysconfig/varnish 配置服务端口
[root@lue3 varnish]# vim /etc/varnish/varnish.params
 14 VARNISH_LISTEN_PORT=80
3.编辑/etc/security/limits.conf
[root@lue3 varnish]# vim /etc/security/limits.conf
varnish  - nofile   131072  #添加
varnish - memlock   90000

  • 展现出Varnish的Cache功能

[root@lue3 varnish]# vim /etc/varnish/default.vcl

vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend web1 {
    .host = "172.25.6.250";
    .port = "80";
}

sub vcl_recv {

}

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.
}

sub vcl_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.
# 查看缓存命中情况
    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);
}
[root@lue3 varnish]# systemctl reload varnish.service
                                        

 测试:

web1服务器的部署 ( ip 172.25.6.250)
1)httpd服务安装
[root@foundation6 ~]yum install httpd -y
[root@foundation6 ~] systemctl start httpd  打开服务
2)默认发布目录下测试内容编写
[root@foundation6 ~] cd /etc/www/html/index.html
[root@foundation6 html] echo www.westos.org > index.html
3)本机域名解析(解析的域名和ip对应的是Varnish服务器)
[root@foundation6 ~] vim /etc/hosts
172.25.6.5 lue3 www.westos.org    #添加

客户端可以通过Varnish服务器来实现访问后端web服务器的功能 

  • 当出现多个不同域名后端web服务器时的配置

vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend web1 {
    .host = "172.25.6.250";#第一个后端服务器
    .port = "80";
}
backend web2 {
    .host = "172.25.6.6"; #第二个后端服务器
    .port = "80";
}
#当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据,
 # 访问其他页面报错。
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"));
        }

}

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.
}

sub vcl_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.

    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);
}
                                                                                                                                   
[root@lue3 varnish]# systemctl restart varnish.service

测试:当出现多个不同域名后端web服务器时的配置

lue4上有nignx,安装及配置参考链接https://blog.csdn.net/lcqrehl/article/details/107922976
[root@lue4 ~]# cd /usr/local/lnmp/nginx/html  
[root@lue4 html]# echo bbs.westos.org > index.html 
[root@lue4 html]# cd ..
[root@lue4 nginx]# cd sbin
[root@lue4 sbin]# ./nginx
域名解析(解析的域名和ip对应的是Varnish服务器)
[root@foundation6 ~]# vim /etc/hosts
172.25.6.5 lue3 www.westos.org bbs.westos.org   #添加地址解析

 

  • 健康监测和负载均衡

[root@lue3 varnish]# vim /etc/varnish/default.vcl
vcl 4.0;

import directors;
# Default backend definition. Set this to point to your content server.
probe backend_healthcheck {    #probe:定义健康状态检测方法
    .url = "/index.html";      #哪个 url 需要 varnish 请求
    .window = 3;               #window:基于最近的多少次检查来判断其健康状态;
    .threshold = 2;            #threshhold:最近.window中定义的这么次检查中至有.threshhold定义的次数是成功的;
    .interval = 3s;            #interval:检测频度;
}
backend web1 {
    .host = "172.25.6.250";
    .port = "80";
    .probe = backend_healthcheck;
}
backend web2 {
    .host = "172.25.6.6";
    .port = "80";
    .probe = backend_healthcheck;
}

sub vcl_init {      # 把多个后端聚合为一个组
        new web_cluster = directors.round_robin();
        web_cluster.add_backend(web1);
        web_cluster.add_backend(web2);
}
sub vcl_recv {
        if (req.http.host ~ "^(www.)?westos.org") {
                set req.http.host = "www.westos.org";
                set req.backend_hint = web_cluster.backend();
                return(pass);    # 为了测试方便,不进行缓存
        }
        elseif (req.http.host ~ "^bbs.westos.org") {
                set req.backend_hint = web2;
                #return(pass);
        }
}

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.
}

sub vcl_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.

    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);
}
[root@lue3 varnish]# systemctl restart varnish.service #重启服务

客户端测试:后端的两个web服务器轮询的方式出现

三、基于varnish的代理

[root@lue3 ~]# vim /etc/varnish/default.vcl

vcl 4.0;

import directors;
# Default backend definition. Set this to point to your content server.
probe backend_healthcheck {
    .url = "/index.html";
    .window = 3;
    .threshold = 2;
    .interval = 3s;
}
backend web1 {
    .host = "172.25.6.250";
    .port = "80";
    .probe = backend_healthcheck;
}
backend web2 {
    .host = "172.25.6.6";
    .port = "80";
    .probe = backend_healthcheck;
}

sub vcl_init {
        new web_cluster = directors.round_robin();
        web_cluster.add_backend(web1);
        web_cluster.add_backend(web2);
}
sub vcl_recv {
        if (req.http.host ~ "^(www.)?westos.org") {
                set req.http.host = "www.westos.org";
                set req.backend_hint = web_cluster.backend();
                #return(pass);
        }
        elseif (req.http.host ~ "^bbs.westos.org") {
                set req.backend_hint = web2;
                return(pass);
        }
        else {
                return(synth(404,"Not in cache"));
        }

}

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.
}

[root@lue3 varnish]# systemctl restart varnish

 2.在lue4中做反向代理

在lue4中
[root@lue4 ~]# vim /usr/local/lnmp/nginx/conf/nginx.conf
server {
        listen 80;
        server_name bbs.westos.org;

        location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://172.25.6.7;
        }
}

[root@lue4 conf]# cd ../sbin/
[root@lue4 sbin]# ./nginx -s reload  #(/usr/local/lnmp/nginx/sbin/nginx -s reload)

2. lue5记录代理过程做的配置

再开一台虚拟机lue5:172.25.6.7   #注:80端口没有被占用
从lue3中远程传输nginx的压缩包
[root@lue3 ~]# scp nginx-1.18.0.tar.gz lue5:/root
tar zxf nginx-1.18.0.tar.gz
[root@lue5 ~]# cd nginx-1.18.0/
[root@lue5 ~]# yum install -y gcc pcre-devel openssl-devel
[root@lue5 ~]# ./configure --prefix=/usr/local/lnmp/nginx/ --with-http_ssl_module  --with-http_realip_module
[root@lue5 ~]# make && make install
[root@lue5 ~]# vim /usr/local/lnmp/nginx/conf/nginx.conf   #添加server字段
server {
        listen       80;
        server_name  localhost;
        real_ip_header    X-Forwarded-For;
        real_ip_recursive on;
        set_real_ip_from 172.25.6.0/24;
[root@lue5 conf]# ../sbin/nginx  #开启nginx

3.客户端测试:

1.在lue4中做地址解析
[root@lue4 conf]# vim  /etc/hosts
172.25.6.6 lue4 bbs.westos.org   #添加
2.在lue5中
[root@lue5 ~]# cd /usr/local/lnmp/nginx/html
[root@lue5 html]# echo bbs.westos.org -lue5 > index.html
[root@lue5 html]# cat index.html
bbs.westos.org -lue5
[root@lue5 conf]# ../sbin/nginx -s reload

在有解析的主机上,[root@foundation6 html]# curl www.westos.org

 查看lue5上的日志

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值