架构--缓存知识
缓存体系是根据http体系来写的
nginx代理缓存配置 mkdir -p /data/cdn_cache ---------------------------------------------------------------------------------------------------------- [root@linux-node1 ~]#/etc/nginx/conf.d/proxy.conf #CDN proxy_temp_path /data/cdn_cache/proxy_temp_dir; proxy_cache_path /data/cdn_cache/proxy_cache_dir levels=1:2 keys_zone=cache_one:50m inactive=1d max_size=1g; proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404; ---------------------------------------------------------------------------------------------------------- [root@linux-node1 ~]# /etc/nginx/conf.d/upstream.conf upstream www.example.com.pool { server 192.168.56.12:8080 weight=10 max_fails=3; } ---------------------------------------------------------------------------------------------------------- [root@linux-node1 ~]# vim /etc/nginx/conf.d/www.example.com.conf server { listen 80; server_name www.example.com; access_log logs/www.example.com-access.log main; location ~ .*\.(gif|jpg|png|html|htm|css|js|ico|swf|pdf)$ { #Proxy proxy_redirect off; proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-real-ip $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://www.example.com; #Use Proxy Cache proxy_cache cache_one; proxy_cache_key "$host$request_uri"; add_header Cache "$upstream_cache_status"; proxy_cache_valid 200 304 301 302 8h; proxy_cache_valid 404 1m; proxy_cache_valid any 2d; } location / { proxy_redirect off; proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-real-ip $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://www.example.com; client_max_body_size 40m; client_body_buffer_size 128k; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffer_size 64k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; } } ------------------------------------------------------------------------------------------------ [root@linux-node2 ~] vim vhost.example.com.conf server { listen 8080; server_name www.example.com; location / { root /data/www/www; index index.html index.htm; } error_log logs/error_www.example.com.log error; access_log logs/access_www.example.com.log main; } windoes 绑定主机名访问 www.example.com,返回node1服务器发现有缓存了
如果用户访问后就回有缓存,但是如果网页文件跟新了,但是用户还是访问的缓存这样就回出现问题,则需要删缓存流程:
#!/bin/bash cache_purge(){ PURGE_URL=$1 URL_NAME=$(echo -n $PURGE_URL | md5sum | awk '{print $1}') FILE_NAME=$(echo $URL_NAME | awk '{print "/data/cdn_cache/proxy_cache_dir/"substr($0,length($0),1)"/"substr($0,length($0)-2,2)"/"$0}') rm -rf $FILE_NAME } purge_file(){ PURGE_FILE=$1 for url in $(cat $PURGE_FILE);do cache_purge $url done } purge_url(){ PURGE_URL=$1 cache_purge $PURGE_URL } usage(){ echo $"Usage: $0 <url_file | 'url'>" } main (){ if [ "$#" -ne 1 ];then usage; else if [ -f $1 ];then purge_file $1; else purge_url $1; fi fi } main $1
[root@node1 ~]# sh a.sh www.example.com/index.html
通过MD5的方式找到缓存池的缓存,并把缓存删了
####web缓存
操作码缓存 ----php配置可以开启opcache http://www.php.net/manual/zh/book.opcache.php
http://www.php.net/manual/zh/opcache.configuration.php
web服务器缓存--apache(mod_cache),nginx(fastCGI缓存,proxy cache)
####应用层
|----动态内容缓存 缓存动态输出
应用服务--- |----页面静态化 动态页面静态化,需要专门用于静态化的CMS
|----local cache 应用本地缓存,php(yac,xcache) java (ehcache)
|----分布式缓存 mamcache,radis等
数据层----- |----数据库mysql mysql自身缓存,innodb缓存,MYISAM缓存
系统层----- |---操作系统-----cpu cache L1(数据缓存,指令缓存)L2 L3
-----内存 cache 内存高速缓存,page cache
物理层----- | ----raid卡-----raid cache 磁盘整列缓存(raid卡可以控制是否实用磁盘高速缓存)
| ----磁盘 ---- disk cache 磁盘高速缓存