Showing posts with label cache. Show all posts
Showing posts with label cache. Show all posts

Friday, March 11, 2011

Caching WordPress with Varnish

WordPress is a fantastic application that lets you get a website up and running in a matter of minutes. Not only that, but you require absolutely no knowledge of how to code to do so. As with many web applications though, it needs a little help in getting it to perform better. Lots of these dynamic apps breaks caching by using lots of cookies and setting headers like Cache-Control and Pragma. For a better in-depth tutorial on caching, read this article.

Requirements



  • Web server running on a different port than the standard one (TCP/80)

  • Varnish installed


Configuration


The standard location for Varnish’s configuration file is located at/etc/varnish/default.vcl. You will find below the configuration I use on this blog. See the comments to get a better understanding of what’s what. Also, make sure varnish will listen on port TCP/80 (use the -a flag when you start the daemon).
# Send all requests to your webserver
backend default {
.host = "127.0.0.1";
.port = "8080";
}

sub vcl_recv {
# If we don't set this, in our webserver's logs, we'll get varnish's IP instead
# of the real client's IP
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For ", " client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
# Unless we're in the admin/login section, remove all cookies
if (!(req.url ~ "wp-(login|admin)")) {
unset req.http.cookie;
}
return (lookup);
}

sub vcl_pipe {
return (pipe);
}

sub vcl_pass {
return (pass);
}

sub vcl_hash {
set req.hash += req.url;
if (req.http.host) {
set req.hash += req.http.host;
} else {
set req.hash += server.ip;
}
return (hash);
}

sub vcl_hit {
if (!obj.cacheable) {
return (pass);
}
return (deliver);
}

sub vcl_miss {
return (fetch);
}

sub vcl_fetch {
if (!beresp.cacheable) {
return (pass);
}
if (beresp.http.Set-Cookie) {
return (pass);
}
if (!(req.url ~ "wp-(login|admin)")) {
unset req.http.cookie;
}
return (deliver);
}

sub vcl_deliver {
# Remove bad headers
remove resp.http.X-Varnish;
remove resp.http.Via;
remove resp.http.Age;
remove resp.http.X-Powered-By;
remove resp.http.Cache-Control;
remove resp.http.Pragma;
return (deliver);
}

Restart Varnish to load the new configuration.

Testing the recipe


Open up varnishstat in your terminal. You should see some stats. Ctrl-F5 a few times your site in your favorite browser. That way, you’re telling your browser to bypass its own cache and ask the site for fresh items. The first time you access your site, you will get lots of misses, but after that, you should get lots of cache hits.
Hitrate ratio:        9        9        9
Hitrate avg:     0.7302   0.7302   0.7302
22         0.00         0.16 Client connections accepted
132         0.00         0.95 Client requests received
100         0.00         0.72 Cache hits
5         0.00         0.04 Cache hits for pass
25         0.00         0.18 Cache misses

As you can see, I’m getting about 4 times more cache hits than misses. This way, Varnish will server all the static items and will let your webserver take care of all the dynamic content. Hope this helps a bit.

 

Wednesday, December 22, 2010

Nginx上ETag支持

[cc lang="text"]
location ~* ^.+.(gif|jpg|css|js|png)$ {
root /var/web/static;
FileETag on;
etag_format \"%X%X\";
add_header Cache-Control max-age=0;
}
[/cc]
这样就可以返回304啦。

Thursday, August 12, 2010

CACHE CONTROL WITH HTACCESS

[cc lang="apache"]
# Cache Control with .htaccess EXPIRES BY TYPE
# from http://forum.powweb.com/showthread.php?t=62786

### turn on the Expires engine
ExpiresActive On

### expires after a month in the client's cache
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType application/pdf A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/plain A2592000

### expires after 4.8 hours
ExpiresByType text/css A17200

# Please note that the "A" before the numbers above stands for Access.
# This means that the stopwatch starts when a client accesses the file.
# You can also use "M" for modified.

# TIME CHEAT SHEET
# 300 5 MIN
# 600 10 MIN
# 900 15 MIN
# 1800 30 MIN
# 2700 45 MIN
#
# 3600 1 HR
# 7200 2 HR
# 10800 3 HR
# 14400 4 HR
# 18000 5 HR
# 36000 10 HR
# 39600 11 HR
# 43200 12 HR
# 46800 13 HR
# 50400 14 HR
# 54000 15 HR
# 86400 24 HR
#
# 86400 1 DAY
# 172800 2 DAY
# 259200 3 DAY
# 345600 4 DAY
# 432000 5 DAY
# 518400 6 DAY
# 604800 7 DAY
#
# 604800 1 WEEK
# 1209600 2 WEEK
# 1814400 3 WEEK
# 2419200 4 WEEK
#
# 2419200 1 MONTH (FEBRUARY)
# 2505600 1 MONTH (FEBRUARY LEAP YEAR)
# 2592000 1 MONTH (APRIL, JUNE, SEPTEMBER, NOVEMBER)
# 2678400 1 MONTH (JANUARY, MARCH, MAY, JULY, AUGUST, OCTOBER, DECEMBER)
# 31536000 12 MONTH[/cc]
[cc lang="apache"]

Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
[/cc]

Tuesday, July 22, 2008

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


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');
}