项目笔记:4.引入CDN内容分发网络,实现网站静态元素加速

来源:互联网 发布:练口才的软件 编辑:程序博客网 时间:2024/04/28 15:47

项目笔记:4.引入CDN内容分发网络,实现网站静态元素加速 squid


1、在192.168.0.6和192.168.0.7上安装squid

开两台虚拟机ip为0.6,0.7 只需搭建本地系统的yum源

# yum -y install squid

一台 父squid和一台子squid多重缓存加速访问。如果访问子squid,子squid没有就会访问父squid,层层上去,父squid没有才去访问原始地址。


2、在父squid192.168.0.6上配置

<pre name="code" class="html"># vim /etc/squid/squid.confacl manager proto cache_objectacl localhost src 127.0.0.1/32 ::1acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1acl localnet src 10.0.0.0/8# RFC1918 possible internal networkacl localnet src 172.16.0.0/12# RFC1918 possible internal networkacl localnet src 192.168.0.0/16# RFC1918 possible internal networkacl localnet src fc00::/7       # RFC 4193 local private network rangeacl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machinesacl SSL_ports port 443acl Safe_ports port 80# httpacl Safe_ports port 21# ftpacl Safe_ports port 443# httpsacl Safe_ports port 70# gopheracl Safe_ports port 210# waisacl Safe_ports port 1025-65535# unregistered portsacl Safe_ports port 280# http-mgmtacl Safe_ports port 488# gss-httpacl Safe_ports port 591# filemakeracl Safe_ports port 777# multiling httpacl Safe_ports port 3130#icpacl CONNECT method CONNECThttp_access allow manager localhosthttp_access deny managerhttp_access deny !Safe_portshttp_access deny CONNECT !SSL_portshttp_access allow localnethttp_access allow localhosthttp_access deny allhttp_port 3128 vhost vporticp_port 3130hierarchy_stoplist cgi-bin ?cache_mem 100 MBcache_dir ufs /var/spool/squid 100 16 256coredump_dir /var/spool/squidrefresh_pattern ^ftp:144020%10080refresh_pattern ^gopher:14400%1440refresh_pattern -i (/cgi-bin/|\?) 00%0refresh_pattern .020%4320cache_peer 192.168.0.2 parent 80 0 no-query originserver no-digest name=w1cache_peer 192.168.0.2 parent 80 0 no-query originserver no-digest name=w2cache_peer 192.168.0.3 parent 80 0 no-query originserver no-digest name=w3cache_peer 192.168.0.3 parent 80 0 no-query originserver no-digest name=w4cache_peer 192.168.0.6 sibling 3128 3130 name=cache0  --父squidcache_peer 192.168.0.7 sibling 3128 3130 name=cache1  <span style="font-family: Arial, Helvetica, sans-serif;">-子squid</span>cache_peer_domain w1bbs1.kong.com cache_peer_domain w2bbs2.kong.comcache_peer_domain w3bbs1.kong.comcache_peer_domain w4bbs2.kong.com

3、在子squid192.168.0.7配置

# vim /etc/squid/squid.confacl manager proto cache_objectacl localhost src 127.0.0.1/32 ::1acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1acl localnet src 10.0.0.0/8# RFC1918 possible internal networkacl localnet src 172.16.0.0/12# RFC1918 possible internal networkacl localnet src 192.168.0.0/16# RFC1918 possible internal networkacl localnet src fc00::/7       # RFC 4193 local private network rangeacl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machinesacl SSL_ports port 443acl Safe_ports port 80# httpacl Safe_ports port 21# ftpacl Safe_ports port 443# httpsacl Safe_ports port 70# gopheracl Safe_ports port 210# waisacl Safe_ports port 1025-65535# unregistered portsacl Safe_ports port 280# http-mgmtacl Safe_ports port 488# gss-httpacl Safe_ports port 591# filemakeracl Safe_ports port 777# multiling httpacl Safe_ports port 3130#icpacl CONNECT method CONNECThttp_access allow manager localhosthttp_access deny managerhttp_access deny !Safe_portshttp_access deny CONNECT !SSL_portshttp_access allow localnethttp_access allow localhosthttp_access deny allhttp_port 3128 vhost vporticp_port 3130hierarchy_stoplist cgi-bin ?cache_mem 100 MBcache_dir ufs /var/spool/squid 100 16 256coredump_dir /var/spool/squidrefresh_pattern ^ftp:144020%10080refresh_pattern ^gopher:14400%1440refresh_pattern -i (/cgi-bin/|\?) 00%0refresh_pattern .020%4320cache_peer 192.168.0.6 parent 3128 0 no-query originserver no-digest name=w1cache_peer 192.168.0.6 parent 3128 0 no-query originserver no-digest name=w2cache_peer 192.168.0.7 sibling 3128 3130 name=cache1 <span style="font-family: Arial, Helvetica, sans-serif;">--子squid</span>cache_peer 192.168.0.6 sibling 3128 3130 name=cache0 <span style="font-family: Arial, Helvetica, sans-serif;">--父squid</span>cache_peer_domain w1bbs1.kong.comcache_peer_domain w2bbs2.kong.com



做测试一级squid

# vim /etc/hosts --记得修改访问端的hosts文件啊,不然什么都别谈了192.168.0.6     bbs1.kong.com      bbs2.kong.com

先访问下http://bbs1.kong.com:3128 3128是squid的端口<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(240, 240, 240);"></span>

# curl -I http://bbs1.kong.com:3128/template/default/image/guest_bg.jpgHTTP/1.0 200 OKServer: nginx/1.4.7Date: Fri, 06 Feb 2015 08:04:35 GMTContent-Type: image/jpegContent-Length: 37373Last-Modified: Wed, 23 Mar 2011 06:33:46 GMTETag: "4d89944a-91fd"Accept-Ranges: bytesX-Cache: MISS from vm06.kong.com--第一次是missX-Cache-Lookup: MISS from vm06.kong.com:3128Via: 1.0 vm06.kong.com (squid/3.1.10)Connection: keep-alive# curl -I http://bbs1.kong.com:3128/template/default/image/guest_bg.jpgHTTP/1.0 200 OKServer: nginx/1.4.7Date: Fri, 06 Feb 2015 08:04:35 GMTContent-Type: image/jpegContent-Length: 37373Last-Modified: Wed, 23 Mar 2011 06:33:46 GMTETag: "4d89944a-91fd"Accept-Ranges: bytesAge: 50X-Cache: HIT from vm06.kong.com--第二次以后都是hitX-Cache-Lookup: HIT from vm06.kong.com:3128Via: 1.0 vm06.kong.com (squid/3.1.10)Connection: keep-alive


测试二级squid

# vim /etc/hosts --记得修改访问端的hosts文件啊,不然什么都别谈了192.168.0.7     bbs1.kong.com      bbs2.kong.com

先访问下http://bbs1.kong.com:3128 3128是squid的端口

# curl -I http://bbs1.kong.com:3128/template/default/image/guest_bg.jpgHTTP/1.0 200 OKServer: nginx/1.4.7Date: Fri, 06 Feb 2015 08:04:35 GMTContent-Type: image/jpegContent-Length: 37373Last-Modified: Wed, 23 Mar 2011 06:33:46 GMTETag: "4d89944a-91fd"Accept-Ranges: bytesAge: 230X-Cache: HIT from vm06.kong.com --hit来时父squidX-Cache-Lookup: HIT from vm06.kong.com:3128X-Cache: MISS from vm07.kong.comX-Cache-Lookup: MISS from vm07.kong.com:3128Via: 1.0 vm06.kong.com (squid/3.1.10), 1.0 vm07.kong.com (squid/3.1.10)Connection: keep-alive# curl -I http://bbs1.kong.com:3128/template/default/image/guest_bg.jpgHTTP/1.0 200 OKServer: nginx/1.4.7Date: Fri, 06 Feb 2015 08:04:35 GMTContent-Type: image/jpegContent-Length: 37373Last-Modified: Wed, 23 Mar 2011 06:33:46 GMTETag: "4d89944a-91fd"Accept-Ranges: bytesX-Cache: HIT from vm06.kong.com --hit来时父squidX-Cache-Lookup: HIT from vm06.kong.com:3128Age: 265X-Cache: HIT from vm07.kong.com X-Cache-Lookup: HIT from vm07.kong.com:3128Via: 1.0 vm06.kong.com (squid/3.1.10), 1.0 vm07.kong.com (squid/3.1.10)Connection: keep-alive

</pre><p></p><pre>
4、在192.168.0.1上的nginx增加squid的分发

# vim /etc/nginx/nginx.confuser  nobody nobody;worker_processes  4;error_log  /var/log/nginx/error.log  info;pid        /var/run/nginx.pid;events {    worker_connections  15000;}http {    include       mime.types;    default_type  application/octet-stream;    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    access_log  /var/log/nginx/access.log  main;    sendfile        on;    tcp_nopush     on;    keepalive_timeout  30;    gzip  on;upstream squid_upstream {server 192.168.0.6:3128;server 192.168.0.7:3128;hash        $request_uri;        hash_again  10;}upstream tomcat_upstream {server 192.168.0.2:8080 weight=10;server 192.168.0.3:8080 weight=10;}    server {        listen       80;        server_name  localhost;        charset utf8;location / {index  index.html index.htm index.jsp;proxy_pass http://squid_upstream;                proxy_set_header Host $host;                proxy_set_header X-Forward-For $remote_addr;        }location ~ \.jsp$ {proxy_pass http://tomcat_upstream;                proxy_set_header Host $host;                proxy_set_header X-Forward-For $remote_addr;        }        location ~ ^/forum-[0-9]-[0-9]-[0-9]\.html$ {                proxy_pass http://tomcat_upstream;                proxy_set_header Host $host;                proxy_set_header X-Forward-For $remote_addr;        }    }}# service nginx reload


最后修改hosts 指向192.168.0.1


0 0
原创粉丝点击