随着访问量的增加,后端web与数据库负载不断升高,因此在前端增加了页面缓存层,效果良好。实现方式如下:

整个系统架构中,最前端是nginx反向代理,反向代理之后,有squid页面缓存,之后为web server,页面缓存对指定的url进行(此功能在nginx上实现)

规则:对登录用户直接反向代理到web,对非登录用户,先走squid,squid中有则直接返回,若无,反向代理到web。具体的配置实现如下:

nginx配置,因nginx不支持if嵌套,因此配置如下:
InBlock.gif     location ~* /(c[0-9]+-[0-9]+.html|[c|d|g][0-9]+.html)
InBlock.gif                {
InBlock.gif                proxy_set_header Host    $host;
InBlock.gif                proxy_set_header X-Forwarded-For    $remote_addr;
InBlock.gif
                set $neirong nei_rong_squid;
InBlock.gif                 if ($http_cookie ~* "61fb1_user") {
InBlock.gif                                                set $neirong nei_rong;
InBlock.gif                                }
InBlock.gif                 if ($request_uri ~* /d[0-9]+.html){
InBlock.gif                                                proxy_pass http: //$neirong;
InBlock.gif                                             break;
InBlock.gif                                }
InBlock.gif                proxy_pass http: //nei_rong;
InBlock.gif
                access_log /home/nginx/logs/product_nr;
InBlock.gif                }
若访问的地址是d[0-9]+.html,且cookie中有61fb1_user(判断是否登录,登录帐户有个性化的内容出现,不能缓存)则走squid缓存,否则直接反向代理到web

squid配置文件如下:
InBlock.gifacl all src 0.0.0.0/0.0.0.0
InBlock.gifacl manager proto cache_object
InBlock.gifacl localhost src 127.0.0.1/255.255.255.255
InBlock.gifacl to_localhost dst 127.0.0.0/8
InBlock.gifacl SSL_ports port 443
InBlock.gifacl CONNECT method CONNECT
InBlock.gif
http_access allow all
InBlock.gificp_access allow all
InBlock.gif
#cache_effective_user nobody
InBlock.gif#cache_effective_group nobody
InBlock.gifcache_mgr             liguoqiang@gongchang.com
InBlock.gif
http_port 2000 accel vhost vport
InBlock.gif
cache_peer            192.168.2.51        parent    82 0        originserver         name=gcbuy
InBlock.gifcache_peer            192.168.2.152     parent    99 0        originserver         name=product
InBlock.gifcache_peer            192.168.2.53        parent    998 0     originserver         name=company
InBlock.gifcache_peer            192.168.2.52        parent    82 0        originserver         name=gcns
InBlock.gif
cache_peer_domain company company.ch.gongchang.com
InBlock.gifcache_peer_domain product product.ch.gongchang.com
InBlock.gifcache_peer_domain gcbuy     buy.ch.gongchang.com    
InBlock.gifcache_peer_domain gcns        .cn.gongchang.com
InBlock.gif
refresh_pattern -i product\.ch\.gongchang\.com                    43200        80%        60000     ignore-reload
InBlock.gifrefresh_pattern -i buy\.ch\.gongchang\.com                            43200        80%        60000     ignore-reload
InBlock.gifrefresh_pattern -i company\.ch\.gongchang\.com                    43200        80%        60000     ignore-reload
InBlock.gifrefresh_pattern -i \.cn\.gongchang\.com                                 43200        90%        60000     ignore-reload
InBlock.gifhierarchy_stoplist cgi-bin ?
InBlock.gif
#cache_mem 8192 MB
InBlock.gifcache_mem 4096 MB
InBlock.gifcache_dir ufs /ssd/squid 117760 16 256
InBlock.gifcache_swap_low 80
InBlock.gifcache_swap_high 97
InBlock.gif
maximum_object_size 4096 KB
InBlock.gifminimum_object_size 0 KB
InBlock.gifmaximum_object_size_in_memory 1024 KB
InBlock.gifmemory_pools on
InBlock.gifmemory_pools_limit 512 MB
InBlock.gif
#access_log /var/log/squid/access.log squid
InBlock.gifaccess_log none
InBlock.gifcache_store_log none
InBlock.gifcache_store_log none
InBlock.gifcache_vary on
InBlock.gif
cache_replacement_policy lru
InBlock.gifmemory_replacement_policy lru
InBlock.gif

InBlock.gifacl QUERY urlpath_regex cgi-bin \?
InBlock.gifcache deny QUERY
InBlock.gif
refresh_pattern ^ftp:                     1440        20%         10080
InBlock.gifrefresh_pattern ^gopher:                1440        0%            1440
InBlock.gifrefresh_pattern .                             0             20%         4320
InBlock.gif
#refresh_pattern -i www\.naizhao\.com                     240         100%        1440        ignore-reload
InBlock.gif
acl apache rep_header Server ^Apache
InBlock.gifbroken_vary_encoding allow apache
InBlock.gif
visible_hostname gc_squid_001
InBlock.gif
coredump_dir /var/spool/squid
InBlock.gif
acl Purge method PURGE
InBlock.gifacl clear src 192.168.2.0/24
InBlock.gifhttp_access allow Purge clear
InBlock.gifhttp_access deny Purge
InBlock.gif

InBlock.gifsnmp_port 3401
InBlock.gifacl cactiserver src 127.0.0.1 192.168.2.1 192.168.2.253
InBlock.gifacl SNMP snmp_community snmppublic
InBlock.gifsnmp_access allow SNMP cactiserver
InBlock.gifsnmp_access deny all
InBlock.gif
header_access X-Cache-Lookup deny all
InBlock.gifheader_access Via deny all