CDN原理

BIND 视图技术实现DNS,Squid做前端缓存
简单的CDN的搭建:
1、CDN的工作原理:当外网访问内网域名的时候,外网先经过本地的dns查询,然后dns向内网搭建的dns发出申请,内网dns读取配置文件named.conf中包含IP列表的include,如果IP列表中有
该地的IP记录则对应IP列表寻找对应的视图,如没有记录则拒绝返回信息。视图将解析你访问的域名到squid缓存服务器,squid再向对应的web服务器拿取数据。
2、准备工作: 实现简单的CDN只需要4台服务器,squid,web服务器,dns,客服机。客服机、dns和squid中的一个网卡模拟外网,web和squid中的另一个网卡模拟内网。
squid:eth0:192.168.247.13 eth1:192.168.75.131
web : 192.168.75.129
dns : 192.168.247.14
xp : 192.168.247.128
3、搭建web服务: tar zxvf pcre-8.01.tar.gz
cd pcre-8.01
./configure --prefix=/usr && make && make install
tar xvf nginx-0.8.54.tar.gz -C /usr/local/src
cd /usr/local/src
./configure --prefix=/usr/local/nginx
make && make install
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80 ;
server_name www.fengtao5968.com;
index index.html index.htm;
root /usr/local/www/; <--创建www文件夹,存放网页目录
error_page 404 /404.htm;
autoindex_exact_size on;
access_log /usr/local/nginx/logs/ft5968.logs combined;
}
/usr/local/nginx/sbin/nginx 启动nginx
4、搭建squid: vim /etc/security/limits.conf

    • nproc 20480
    • nofile 204800
      ulimit -SHu 20480
      ulimit -SHn 204800
      tar zxvf squid-3.0.STABLE20.tar.gz -C /usr/local/src
      cd /usr/local/src/squid-3.0.STABLE20
      ./configure --prefix=/usr/local/squid && make && make install
      vim /usr/local/squid/etc/squid.conf
      http_port 80 accel vhost vport
      cache_peer 192.168.75.129 parent 80 0 originserver name=web1
      cache_peer_domain web1 www.fengtao5968.com
      cache_mem 512 MB
      maximum_object_size_in_memory 32 KB
      cache_dir diskd /usr/local/squid/var/cache 10240 16 256 Q1=64 Q2=72
      maximum_object_size 200 MB
      logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %h" "%{User-Agent}>h" %Ss:%Sh
      access_log /usr/local/squid/var/logs/access.log combined
      cache_effective_user squid <--如果没有squid用户创建squid用户
      cache_effective_group squid
      visible_hostname sso.ft2022.com <--squid机器的主机名
      http_access allow all
      mkdir -p /usr/local/squid/var/cache
      chown squid:squid /usr/local/squid/var/cache
      mkdir -p /usr/local/squid/var/logs/
      chown squid:squid /usr/local/squid/var/logs/
      /usr/local/squid/sbin/squid -z -X <--初始化缓存目录
      /usr/local/squid/sbin/squid -D <--doman模式下启动
      测试:客服机需要在host中绑定192.168.247.13 www.fengtao5968.com
      5、搭建dns: tar zxvf bind-9.6.0.tar.gz -C /usr/local/src/
      cd /usr/local/src/bind-9.6.0/
      ./configure --prefix=/usr/local/named && make && make install
      cd /usr/local/named/
      sbin/rndc-confgen -a <--生成rndc.key
      cd etc/
      ../sbin/dnssec-keygen -a hmac-md5 -b 128 -n HOST fengtao5968 <--生成一个秘钥,提供给视图使用。该实验没有使用视图的秘钥。
      dig > named.ca
      vim named.conf
      options {
      directory "/usr/local/named/etc"; <--要写named安装路径下的named.conf
      allow-query-cache {any;};
      pid-file "named.pid";
      include "rndc.key";
      controls {
      inet 127.0.0.1 allow { localhost; } keys { rndc-key; };
      };
      logging {
      channel query_log {
      file "query.log" versions 3 size 20m;
      severity info;
      print-time yes;
      print-category yes;
      };
      category queries {
      query_log;
      };
      };
      include "ip.fengtao5968"; vim /usr/local/named/etc/ip.fengtao5968
      view "view_fengtao5968" { acl "FENGTAO5968" {
      match-clients { FENGTAO5968; }; 192.168.24.0/24;
      recursion yes; 192.168.247.0/24;<-- 一定要包含本机的网段,否则dns无法解析到视图。
      zone "." IN { };
      type hint;
      file "named.ca";
      };
      zone "localhost" IN { vim /usr/local/named/etc/localhost.zone
      type master; ORIGIN localhost.
      allow-update { none; }; @ 1D IN SOA @ root (
      }; 42 ; serial (d. adams)
      zone "0.0.127.in-addr.arpa" IN { 3H;
      type master; 15M;
      file "named.local"; 1W;
      allow-update { none; }; 1D )
      }; 1D IN NS @
      zone "fengtao5968.com" IN{ 1D IN A 127.0.0.1
      type master;
      file "fengtao5968.conf";
      allow-update { none; };
      };
      };
      vim /usr/local/named/etc/named.local
      TTL 86400 @ IN SOA localhost. root.localhost. ( 2013040610 ; Serial <--这个参数要和下面fengtao5968.conf一样 28800 ; Refresh 14400 ; Retry 3600000 ; Expire 86400 ) ; Minimum IN NS localhost. 1 IN PTR localhost. vim /usr/local/named/etc/fengtao5968.confTTL 180
      $ORIGIN fengtao5968.com.
      @ IN SOA @ root. ( 实验中不要用域名替代第二个@,否则会出现找不到相应域名的错误日志。
      2013040610
      180
      900
      3600000
      3600 )
      NS @
      www CNAME ftp.fengtao5968.com.
      ftp.fengtao5968.com. IN A 192.168.247.13
      测试:/usr/local/named/sbin/named-checkconf /usr/local/named/etc/named.conf
      启动:/usr/local/named/sbin/named
      查看系统日志:tail -f /var/log/messages 若无错误实验基本上成功
      检测域名是否可用:dig www.fengtao5968.com 若被检测到squid的IP上去就表示成功
      6、测试: 将客服机的dns指向dns服务器即可。

你可能感兴趣的:(CDN原理)