On Varnish’s official website, there is a WordPress optimization guide For The Impatient: Preparing Varnish/Wordpress for a Slashdotting in 60 seconds or less….
The problem is that it removes cookie too aggressively. All non admin page will be virtually static. So I made my own vcl to remove cookies for only static files.
Here it is
backend default {
.host = "10.25.0.1";
.port = "80";
}
sub vcl_recv {
# Normalize Content-Encoding
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|lzma|tbz)(\?.*|)$") {
remove req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
remove req.http.Accept-Encoding;
}
}
# Remove cookies and query string for real static files
if (req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.*|)$") {
unset req.http.cookie;
set req.url = regsub(req.url, "\?.*$", "");
}
# Remove cookies from front page
if (req.url ~ "^/$") {
unset req.http.cookie;
}
}
sub vcl_fetch {
if (req.url ~ "^/$") {
unset obj.http.set-cookie;
}
}
So all interactive pages will be sent to php backend with correct cookies. All static files and front page will be served by varnish proxy.
Yejun Web varnish, vcl, wordpress
After reading some recent benchmark reguarding tcmalloc performance on mysql. I decide to rebuild my whole webhosting stack with it.
ICC is intel’s c++ compiler, which has faster performance is also memtioned on mysql website.
Most distros should already has google performance tools prepackaged. Installation of ICC is slightly more complicated, you can download it directly from intel’s website which is free download for non-commercial use. Archlinux and Gentoo both have packaged installer. On ubuntu/debian system you probably also need build-essential and apt-build to rebuilt packages. On archlinux you will need base-devel and abs.
For most packages, the following bash script can be used before configuration/make step. Don’t ommit the dot on first line and change the path of iccvars.sh to your installation directory.
. /opt/intel/Compiler/11.0/081/bin/iccvars.sh intel64
CC=icc
CFLAGS="-xHOST -O3 -no-prec-div "
LD=xild
AR=xiar
CXX=icpc
CXXFLAGS="-xHOST -O3 -no-prec-div "
LDFLAGS=-ltcmalloc_minimal
export CC CFLAGS LD AR CXX CXXFLAGS LDFLAGS
These setting seems safe for all packages. Here is a summary of package specific cflags setting.
|
Mysql |
Cherokee |
Nginx |
Varnish |
PHP |
Memcached |
-static |
No |
No |
No |
No |
N/A |
No |
-ipo |
No |
No |
Yes |
No |
N/A |
Yes |
LDFLAGS=-ltcmalloc_minimal |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
configure option |
–disable-shared –with-mysqld-libs=-ltcmalloc_minimal |
None |
None |
–disable-jemalloc |
Failed with ICC |
None |
This might disappoint you. But the rebuilt software stacks show no improvement whatsoever in my benchmark.
Yejun Linux, Web cherokee, icc, intel, memcached, mysql, nginx, php, tcmalloc, varnish
There is no built in function to do redirecting in Varnish. After googling around, here is the result.
Using error page of varnish to return an object with location header, since obj is not available within vcl_recv.
sub vcl_recv {
if (req.http.host ~ "^(www.)?mudy.info$")
{
error 302;
}
}
sub vcl_error {
if (obj.status == 302 && req.http.host ~ "^(www.)?mudy.info$")
{
set obj.http.Location = "http://blog.mudy.info/";
}
}
I am unsure whether there is a simpler way to verify the request, so I test the same condition again inside vcl_error.
If you want request url appended to redirection, you can use this instead.
set obj.http.Location = "http://blog.mudy.info" req.url;
Yejun Linux, Web 301, 302, http, redirect, varnish, Web