我自己网站的Varnish + Apache 加速设定

2013 年 5 月 23 日9890

本站主要内容均为原创,转帖需注明出处www.alexclouds.net

      实际使用中发现Varnish的确是能起到有效的作用,因此贴出来以供WP user 参考。Varnish可以有效降低web服务器的负载,提升访问速度。根据官方的说法,Varnish是一个cache型的HTTP反向代理。

以root身份运行如下命令来下载和安装Varnish。
先安装所需环境

# yum update
# yum install gcc make automake autoconf libtool ncurses-devel libxslt groff pcre-devel pckgconfig libedit libedit-devel -y

下载varnish TAR.GZ文件,并解压,进入目录然后

sh autogen.sh
sh configure
make
make install

         Vanish安装在/usr/local目录下。主程序的完整路径为:/usr/local/sbin/varnishd,默认的配置文件/usr/local/etc/varnish/default.vcl在运行varnishd之前,需要先配置后端的web服务器,参照如下格式编辑default.vcl文件,更改为你自己web服务器配置。

        因此先配置一个后端服务器的地址出来,我的APACHE就在本机的81端口。

修改文件 /usr/local/etc/varnish/default.vcl

backend default {
     .host = "127.0.0.1";
     .port = "81";
     .connect_timeout = 600s;
     .first_byte_timeout = 600s;
     .between_bytes_timeout = 600s;
     .max_connections = 800;
}

      修改APACHE服务器的配置

      Listen 81

NameVirtualHost *:81

#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>

<VirtualHost *:81>
DocumentRoot /var/www/html
ServerName www.example2.org
# Other directives here
</VirtualHost>

<VirtualHost *:81>
    DocumentRoot /*/*/alexclouds
    ServerName www.alexclouds.net
    ServerAlias alexclouds.net
</VirtualHost>

       重新启动APAPCHE,APACHE的配置完成。

       在/usr/local/etc/varnish/default.vcl里增加一段针对 WP的优化代码,我原文贴出来了,可以COPY进去直接用。

backend default {
     .host = "127.0.0.1";
     .port = "81";
     .connect_timeout = 600s;
     .first_byte_timeout = 600s;
     .between_bytes_timeout = 600s;
     .max_connections = 800;
}

sub vcl_recv {
# only using one backend
set req.backend = default;
#
# set standard proxied ip header for getting original remote address
set req.http.X-Forwarded-For = client.ip;
#
 
# Remove has_js and CloudFlare/Google Analytics __* cookies.
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js)=[^;]*", "");
# Remove a ";" prefix, if present.
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
 
# logged in users must always pass
if( req.url ~ "^/wp-(login|admin)" || req.http.Cookie ~ "wordpress_logged_in_" ){
return (pass);
}
# accept purges from w3tc and varnish http purge
if (req.request == "PURGE") {
return (lookup);
}
#
 
# allow phpmyadmin
if (req.url ~ "^/phpmyadmin") {
return (pass);
}
 
# don't cache search results
if( req.url ~ "\?s=" ){
return (pass);
}
#
# always pass through posted requests and those with basic auth
if ( req.request == "POST" || req.http.Authorization ) {
return (pass);
}
#
# else ok to fetch a cached page
unset req.http.Cookie;
return (lookup);
}
#
# accept purges from w3tc and varnish http purge
sub vcl_hit {
if (req.request == "PURGE") { purge; }
return (deliver);
}
#
# accept purges from w3tc and varnish http purge
sub vcl_miss {
if (req.request == "PURGE") { purge; }
return (fetch);
}
 
#
#
 
sub vcl_fetch {
 
# allow phpmyadmin
if (req.url ~ "^/phpmyadmin") {
return (hit_for_pass);
}
#
# remove some headers we never want to see
unset beresp.http.Server;
unset beresp.http.X-Powered-By;
#
# only allow cookies to be set if we're in admin area - i.e. commenters stay logged out
if( beresp.http.Set-Cookie && req.url !~ "^/wp-(login|admin)" ){
unset beresp.http.Set-Cookie;
set beresp.ttl = 48h;
}
#
 
# don't cache response to posted requests or those with basic auth
if ( req.request == "POST" || req.http.Authorization ) {
return (hit_for_pass);
}
#
# only cache status ok
if ( beresp.status != 200 ) {
return (hit_for_pass);
}
#
# don't cache search results
if( req.url ~ "\?s=" ){
return (hit_for_pass);
}
#
 
# else ok to cache the response
set beresp.ttl = 24h;
return (deliver);
}
#
#
 
sub vcl_deliver {
# add debugging headers, so we can see what's cached
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
}
else {
set resp.http.X-Cache = "MISS";
}
# remove some headers added by varnish
unset resp.http.Via;
unset resp.http.X-Varnish;
}
#
 
sub vcl_hash {
hash_data( req.url );
# altering hash so subdomains are ignored.
# don't do this if you actually run different sites on different subdomains
if ( req.http.host ) {
hash_data( regsub( req.http.host, "^([^\.]+\.)+([a-z]+)$", "\1\2" ) );
} else {
hash_data( server.ip );
}
# ensure separate cache for mobile clients (WPTouch workaround)
if( req.http.User-Agent ~ "(iPod|iPhone|incognito|webmate|dream|CUPCAKE|WebOS|blackberry9\d\d\d)" ){
hash_data("touch");
}
return (hash);
}

      启动 它,用 varnishstat 命令启动执行的情况,接下来看看 varnishlog 命令:

/usr/local/sbin/varnishd -f /usr/local/etc/varnish/default.vcl -a :80 -P /var/run/varnish.pid -s malloc,256m

     使用AB测试

ab -c 1 -n 1000  http://www.alexclouds.net/?p=1543    varnish加速后,显示速度很快。1000个请求很快完成而且后台负载很低。

varnish1

ab -c 1 -n 1000  http://www.alexclouds.net:81/?p=1543   原始网站地址 ,没有加速,显示速度很慢,且后台负载CPU在90%以上。

varnish2

 

        二者确实存在几十倍的差距。希望我的网站服务器不要再拖后腿啦。如果是WP网站以后可以参考我的配置提升性能。

2 0