Tag Archive for 'squid'

Squid 2.7 通过域名反向代理多个服务器配置

visible_hostname squid1.abc.com

#设定squid的主机名,如无此项squid将无法启动

http_port 80 accel vhost vport

#设定squid为accel加速模式,vhost必须要加.否则将无法将主机头转发至后端服务器,访问时就会出现无法找到主机头的错误

cache_peer 192.168.1.88 parent 80 0 no-query originserver name=contentchina

cache_peer 192.168.1.88 parent 80 0 no-query originserver name=bbs

cache_peer 192.168.1.1 parent 80 0 no-query originserver name=ihompy

#定义不同的父节点,将节点设为no-query以及originserver说明这些节点是实际服务器

cache_peer_domain contentchina aaa.abc.com

cache_peer_domain bbs bbb.abc.com

cache_peer_domain ihompy ccc.abc.com

#设定不同域名转发到不同的cache_peer上,如果没有这项.不同域名的域名可能被分发到同一台服务器上.

acl all src 0.0.0.0/0.0.0.0

no_cache deny all

http_access allow all

#允许所有客户端访问

cache_log /var/log/squid/cache.log

#记录日志

#***********ACL存取控制*************

#acl QueryString url_regex .php?

#***********缓冲存取控制*************

#no_cache deny QueryString

#不对符合QueryString的ACL内容进行缓冲

#***********性能优化配置*************

maximum_object_size 320010 KB

#大于此容量的对象将不会被保存在磁盘上,默认大小是4M,如果squid服务器用于缓冲flash等大型文件,建议将此值变大.否则过大的文件在下次重启后将需要重新获取

maximum_object_size_in_memory 100 KB

#最大位于内存中的对象的大小,默认大小是8K,如果服务器内存很大.可以适当提高此值的大小,建议根据网站的80%图片的大小来定.或者根据WEB服务器实际存取文件中最常访问的文件大小来定制

#***********其他可选配置*************

#dns_nameservers 10.0.0.1 192.172.0.4

#配置DNS服务器地址.获取后端时将从此dns获取IP地址

#cache_mgr [email protected]

#在错误日志中出现的webmaster地址.

Squid缓存PHP页面方法

1
2
3
4
5
<?php
header('Cache-Control: max-age=600, must-revalidate');
header('Pragma: cache');
header('Last-Modified: ' . date('D, d M Y H:i:s') . ' GMT');
header('Expires: ' . date ("D, d M Y H:i:s", time() + 600). " GMT");

Squid对访问特定域名的验证

1
2
3
4
5
6
7
acl web_server src web cache
auth_param basic program /opt/packages/squid/libexec/ncsa_auth /opt/packages/squid/etc/squid_passwd
acl deny_domain dstdomain www.sina.com sina.com
acl ncsa_users proxy_auth REQUIRED
http_access allow !deny_domain
http_access allow web_server
http_access allow ncsa_users

Don’t cache this, squid !

http://ramadhan.opensuse-id.org/2009/03/09/dont-cache-this-squid/

I’m using squid. And for some specific reason, I don’t want squid to cache specific servers. So I put these on my squid.conf :

1
2
acl NOT-TO-CACHE dstdomain "/etc/squid/list/not-to-cache.conf"
no_cache deny NOT-TO-CACHE

and put this domains in /etc/squid/list/not-to-cache.conf

1
2
3
4
5
6
7
planet.terasi.net
blogsome.com
wordcodess.com
blogger.com
dagdigdug.com
opensuse-id.org
technorati.com

Using the same method, I don’t want my squid to cache *.css. So I put these on my squid.conf :

1
2
3
hierarchy_stoplist cgi-bin ?
acl QUERY urlpath_regex cgi-bin \? \.css
no_cache deny QUERY

Simply, then I executed “# squid -k reconfigure

PHP配合Squid缓存动态页面Header写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function nocache_headers() {
       // @ header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
       // @ header('Expires: Wed, 11 Jan 2009 05:00:00 GMT');  
       @ header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
       if(PAGEME=="bookinfo"){
                @ header('Expires: ' . gmdate ("D, d M Y H:i:s", time() + 3600*24). " GMT");
       }else{
                @ header('Expires: ' . gmdate ("D, d M Y H:i:s", time() + 3600). " GMT");      
       }
       
       // @ header('Cache-Control: no-cache, must-revalidate, max-age=0');
       // @ header('Pragma: no-cache');
               
       // @ header('Cache-Control: no-cache, must-revalidate, max-age=0');
       // @ header('Pragma: no-cache');
}

PHP header work with Squid cache server

1
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

对于动态页面,则可以直接通过写入HTTP返回的头信息,比如对于新闻首页index.php可以是20分钟,而对于具体的一条新闻页面可能是1天后过期。比如:在php中加入了1个月后过期:

1
2
// Expires one month later
header("Expires: " .gmdate ("D, d M Y H:i:s", time() + 3600 * 24 * 30). " GMT");

Squid freenode helper!

hi guys, How to control the cache’s life time by urls? such as, if I visit the url “http://example.com/book/123?delcache”, my squid will delete the cache of “http://example.com/book/123″ web page.
id_sonic: write a helper app to do it
ie, a snippet of perl that when it sees a URL suffixed fixed with ?delcache, will drop that from the cache
oh, thanks. I will try the helper .