随着访问量的增加,后端web与数据库负载不断升高,因此在前端增加了页面缓存层,效果良好。实现方式如下:
规则:对登录用户直接反向代理到web,对非登录用户,先走squid,squid中有则直接返回,若无,反向代理到web。具体的配置实现如下:
nginx配置,因nginx不支持if嵌套,因此配置如下:
location ~* /(c[0-9]+-[0-9]+.html|[c|d|g][0-9]+.html)
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
set $neirong nei_rong_squid;
if ($http_cookie ~* "61fb1_user") {
set $neirong nei_rong;
}
if ($request_uri ~* /d[0-9]+.html){
proxy_pass http: //$neirong;
break;
}
proxy_pass http: //nei_rong;
access_log /home/nginx/logs/product_nr;
}
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
set $neirong nei_rong_squid;
if ($http_cookie ~* "61fb1_user") {
set $neirong nei_rong;
}
if ($request_uri ~* /d[0-9]+.html){
proxy_pass http: //$neirong;
break;
}
proxy_pass http: //nei_rong;
access_log /home/nginx/logs/product_nr;
}
squid配置文件如下:
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443
acl CONNECT method CONNECT
http_access allow all
icp_access allow all
#cache_effective_user nobody
#cache_effective_group nobody
cache_mgr liguoqiang@gongchang.com
http_port 2000 accel vhost vport
cache_peer 192.168.2.51 parent 82 0 originserver name=gcbuy
cache_peer 192.168.2.152 parent 99 0 originserver name=product
cache_peer 192.168.2.53 parent 998 0 originserver name=company
cache_peer 192.168.2.52 parent 82 0 originserver name=gcns
cache_peer_domain company company.ch.gongchang.com
cache_peer_domain product product.ch.gongchang.com
cache_peer_domain gcbuy buy.ch.gongchang.com
cache_peer_domain gcns .cn.gongchang.com
refresh_pattern -i product\.ch\.gongchang\.com 43200 80% 60000 ignore-reload
refresh_pattern -i buy\.ch\.gongchang\.com 43200 80% 60000 ignore-reload
refresh_pattern -i company\.ch\.gongchang\.com 43200 80% 60000 ignore-reload
refresh_pattern -i \.cn\.gongchang\.com 43200 90% 60000 ignore-reload
hierarchy_stoplist cgi-bin ?
#cache_mem 8192 MB
cache_mem 4096 MB
cache_dir ufs /ssd/squid 117760 16 256
cache_swap_low 80
cache_swap_high 97
maximum_object_size 4096 KB
minimum_object_size 0 KB
maximum_object_size_in_memory 1024 KB
memory_pools on
memory_pools_limit 512 MB
#access_log /var/log/squid/access.log squid
access_log none
cache_store_log none
cache_store_log none
cache_vary on
cache_replacement_policy lru
memory_replacement_policy lru
acl QUERY urlpath_regex cgi-bin \?
cache deny QUERY
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern . 0 20% 4320
#refresh_pattern -i www\.naizhao\.com 240 100% 1440 ignore-reload
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
visible_hostname gc_squid_001
coredump_dir /var/spool/squid
acl Purge method PURGE
acl clear src 192.168.2.0/24
http_access allow Purge clear
http_access deny Purge
snmp_port 3401
acl cactiserver src 127.0.0.1 192.168.2.1 192.168.2.253
acl SNMP snmp_community snmppublic
snmp_access allow SNMP cactiserver
snmp_access deny all
header_access X-Cache-Lookup deny all
header_access Via deny all
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443
acl CONNECT method CONNECT
http_access allow all
icp_access allow all
#cache_effective_user nobody
#cache_effective_group nobody
cache_mgr liguoqiang@gongchang.com
http_port 2000 accel vhost vport
cache_peer 192.168.2.51 parent 82 0 originserver name=gcbuy
cache_peer 192.168.2.152 parent 99 0 originserver name=product
cache_peer 192.168.2.53 parent 998 0 originserver name=company
cache_peer 192.168.2.52 parent 82 0 originserver name=gcns
cache_peer_domain company company.ch.gongchang.com
cache_peer_domain product product.ch.gongchang.com
cache_peer_domain gcbuy buy.ch.gongchang.com
cache_peer_domain gcns .cn.gongchang.com
refresh_pattern -i product\.ch\.gongchang\.com 43200 80% 60000 ignore-reload
refresh_pattern -i buy\.ch\.gongchang\.com 43200 80% 60000 ignore-reload
refresh_pattern -i company\.ch\.gongchang\.com 43200 80% 60000 ignore-reload
refresh_pattern -i \.cn\.gongchang\.com 43200 90% 60000 ignore-reload
hierarchy_stoplist cgi-bin ?
#cache_mem 8192 MB
cache_mem 4096 MB
cache_dir ufs /ssd/squid 117760 16 256
cache_swap_low 80
cache_swap_high 97
maximum_object_size 4096 KB
minimum_object_size 0 KB
maximum_object_size_in_memory 1024 KB
memory_pools on
memory_pools_limit 512 MB
#access_log /var/log/squid/access.log squid
access_log none
cache_store_log none
cache_store_log none
cache_vary on
cache_replacement_policy lru
memory_replacement_policy lru
acl QUERY urlpath_regex cgi-bin \?
cache deny QUERY
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern . 0 20% 4320
#refresh_pattern -i www\.naizhao\.com 240 100% 1440 ignore-reload
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
visible_hostname gc_squid_001
coredump_dir /var/spool/squid
acl Purge method PURGE
acl clear src 192.168.2.0/24
http_access allow Purge clear
http_access deny Purge
snmp_port 3401
acl cactiserver src 127.0.0.1 192.168.2.1 192.168.2.253
acl SNMP snmp_community snmppublic
snmp_access allow SNMP cactiserver
snmp_access deny all
header_access X-Cache-Lookup deny all
header_access Via deny all
转载于:https://blog.51cto.com/liguxk/651950