<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Linux, Programming, Telephony, Asterisk, Apache, Tips &#38; Tricks &#187; varnish</title>
	<atom:link href="http://shahidz.com/tag/varnish/feed/" rel="self" type="application/rss+xml" />
	<link>http://shahidz.com</link>
	<description>Passionate About Technology</description>
	<lastBuildDate>Sun, 10 Jan 2010 17:38:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>URL Redirecting (301,302) in Varnish</title>
		<link>http://shahidz.com/url-redirecting-301302-in-varnish/</link>
		<comments>http://shahidz.com/url-redirecting-301302-in-varnish/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 17:37:04 +0000</pubDate>
		<dc:creator>Shahid</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[301]]></category>
		<category><![CDATA[302]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[rewrite]]></category>
		<category><![CDATA[url redirection]]></category>
		<category><![CDATA[varnish]]></category>
		<category><![CDATA[vcl]]></category>

		<guid isPermaLink="false">http://shahidz.com/url-redirecting-301302-in-varnish/</guid>
		<description><![CDATA[There is no straight forward method for 301/302 URL redirection as we did with rewrite rule. But there are some situations where we need to redirect a URL on a host to some URL on different host. In this situation we need to use 301/302 redirection.  There are two ways we can implement this.
First method [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">There is no straight forward method for 301/302 URL redirection as we did with <a href="http://shahidz.com/apache-url-rewriting/" title="Rewrite Rule" target="_blank">rewrite rule</a>. But there are some situations where we need to redirect a URL on a host to some URL on different host. In this situation we need to use 301/302 redirection.  There are two ways we can implement this.</p>
<p align="justify">First method is to use  <em>set req.http.host</em> and <em>set req.url</em> in the vcl.conf file. Using these two methods we perform normal redirection. Using these method we will redirect the url to some other URL on the same host. For example say the URL http://example.com/error500.php will be redirect to the URL http://example.com/redirect.php. We specify 301/302 redirection to the URL http://foo.com/index.php in the redirect.php file using header method.</p>
<ul>
<li>In the sub vcl_recv method use the following code.</li>
</ul>
<pre>sub vcl_recv {
   if (req.http.host ~ "example.com") {
      if (req.url ~ "error500.php") {
         set req.http.host = "example.com";
         set req.url = "/redirect.php";
      }
   }
}</pre>
<ul>
<li>In the redirect.php file use the following method.</li>
</ul>
<pre>&lt;?php
header("HTTP/1.1 302 Moved Temperarly");
header('Location:http://foo.com/index.php');
exit;
?&gt;</pre>
<p>Second method is to use <em>vcl_error</em> error method. In this error method we can specify HTML tags, so using meta tags we can implement redirection. This option is available with the latest version of varnish.</p>
<ul>
<li>In the sub vcl_recv method use the following code.</li>
</ul>
<pre>sub vcl_recv {
   if (req.http.host ~ "example.com") {
      if (req.url ~ "error500.php")
         error;
      }
   }
}
sub vcl_error {
        set obj.http.Content-Type = "text/html; charset=utf-8";
        if (req.http.host ~ "example.com") {
                if (req.url ~ "error500.php") {
                        synthetic {"
                                &lt;?xml version="1.0" encoding="utf-8"?&gt;
                                &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
                                &lt;html&gt;
                                        &lt;head&gt;
                                                &lt;meta http-equiv="refresh" content="0;url=http://foo.com/index.php"&gt;
                                        &lt;/head&gt;
                                &lt;/html&gt;
                        "};
                }
        }
        deliver;
}</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fshahidz.com%2Furl-redirecting-301302-in-varnish%2F&amp;linkname=URL%20Redirecting%20%28301%2C302%29%20in%20Varnish"><img src="http://shahidz.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://shahidz.com/url-redirecting-301302-in-varnish/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Varnish Configuration and Settings</title>
		<link>http://shahidz.com/varnish-configuration-and-settings/</link>
		<comments>http://shahidz.com/varnish-configuration-and-settings/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 15:42:40 +0000</pubDate>
		<dc:creator>Shahid</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[reverse proxy]]></category>
		<category><![CDATA[varnish]]></category>
		<category><![CDATA[varnishd]]></category>

		<guid isPermaLink="false">http://shahidz.com/varnish-configuration-and-settings/</guid>
		<description><![CDATA[
!-- 		@page { size: 21cm 29.7cm; margin: 2cm } 		P { margin-bottom: 0.21cm } 	--> 	
Varnish is a reverse proxy or http accelerator that uses serverside caching to increase the speed of servicing HTTP requests for use in things such as websites. The varnishd daemon accepts HTTP requests from clients, passes them on to  [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css"></style>
<style type="text/css">!-- 		@page { size: 21cm 29.7cm; margin: 2cm } 		P { margin-bottom: 0.21cm } 	--> 	</style>
<p>Varnish is a <a href="http://swik.net/reverse-proxy">reverse proxy</a> or <a href="http://swik.net/http-accelerator">http accelerator</a><meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" /> that uses serverside caching to increase the speed of servicing HTTP requests for use in things such as websites. The varnishd daemon accepts HTTP requests from clients, passes them on to  a backend server and caches the returned documents to better satisfy  future requests for the same document.</p>
<p style="margin-bottom: 0cm" align="justify">Varnish uses Varnish Configuration Language (VCL). The VCL language is a small domain-specific language designed to be used to define request handling and document caching policies for the Varnish HTTP accelerator. When a new configuration is loaded, the varnishd management process translates the VCL code to C and compiles it to a shared object which is then dynamically linked into the server process.</p>
<p align="justify">Varnish uses two configuration file one is VCL and another one contains the values which will be passed as parametes to the varnishd.</p>
<p align="justify">/etc/default/varnish</p>
<pre align="justify"># Configuration file for varnish

# Main configuration file. You probably want to change it <img src='http://shahidz.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
VARNISH_VCL_CONF=/etc/varnish/vcl.conf

# Default address and port to bind to
VARNISH_LISTEN_ADDRESS=0.0.0.0
VARNISH_LISTEN_PORT=80

# Telnet admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082

# The minimum number of threads to start
VARNISH_MIN_WORKER_THREADS=1

# Maximum number of worker threads or INF for unlimited
VARNISH_MAX_WORKER_THREADS=2048

# Timeout value in seconds for threads to return
VARNISH_WORKER_THREAD_TIMEOUT=5

# Hash algorithm to be used
VARNISH_HASHOPTION=classic

# Maximum size of the backend storagefile in bytes
VARNISH_BACKEND_STORAGE_SIZE=10240000
VARNISH_BACKEND_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin

# Backend storage specification
VARNISH_BACKEND_STORAGE="file,${VARNISH_BACKEND_STORAGE_FILE},${VARNISH_BACKEND_STORAGE_SIZE}"

# Set default ttl in secounds
VARNISH_TTL=120</pre>
<p>/etc/varnish/vcl.conf</p>
<pre>      backend default {
        # Our default backend, i.e. the web server
        # You can use more than 1. See docs.
        set backend.host = "0.0.0.0";
        set backend.port = "8080";  # Web Server Port
        }
        sub vcl_recv {
           if ((req.http.host ~ "^backend1..varnish.com") || (req.http.host ~ "^backend2.varnish.com")) {
             pass;
           } else {
             if (req.request != "GET" &amp;&amp; req.request != "HEAD") {
                 pipe;
             }
             if (req.request == "POST") {
                 pass;
             }
             if (req.request == "GET" &amp;&amp; req.url ~ "\.(jpg|jpeg|gif|ico)$") {
                 lookup;
             }
             if (req.request == "GET" &amp;&amp; req.url ~ "\.(css|js)$") {
                 lookup;
             }
             if (req.request == "GET" &amp;&amp; req.url ~ "\.(php|html)$") {
                 lookup;
             }
             if (req.request == "GET") {
                 lookup;
             }
             lookup;
          }
        }
         sub vcl_pipe {
             pipe;
         }

         sub vcl_pass {
             pass;
         }

         sub vcl_hit {
             if (!obj.cacheable) {
                 pass;
             }
             if (req.http.Cookie) {
                pass;
             }
             deliver;
         }

         sub vcl_miss {
             fetch;
         }

         sub vcl_fetch {
             if (!obj.valid) {
                 error;
             }
             if (!obj.cacheable) {
                 pass;
             }
             insert;
         }

         sub vcl_deliver {
             deliver;
         }

         sub vcl_timeout {
             discard;
         }

         sub vcl_discard {
             discard;
         }</pre>
<p>You can use varnishstat to see the status of varnish</p>
<pre>$ varnishstat
client_conn                 0         0.00 Client connections accepted
client_req                  0         0.00 Client requests received
cache_hit                   0         0.00 Cache hits
cache_hitpass               0         0.00 Cache hits for pass
cache_miss                  0         0.00 Cache misses
backend_conn                0         0.00 Backend connections success
backend_fail                0         0.00 Backend connections failures
backend_reuse               0         0.00 Backend connections reuses
backend_recycle             0         0.00 Backend connections recycles
backend_unused              0         0.00 Backend connections unused
n_srcaddr                   0          .   N struct srcaddr
n_srcaddr_act               0          .   N active struct srcaddr
n_sess_mem                  1          .   N struct sess_mem
n_sess                      1          .   N struct sess
n_object                    0          .   N struct object
n_objecthead                0          .   N struct objecthead
n_smf                       1          .   N struct smf
n_smf_frag                  0          .   N small free smf
n_smf_large                 1          .   N large free smf
n_vbe_conn                  0          .   N struct vbe_conn
n_wrk                       0          .   N worker threads
n_wrk_create                0         0.00 N worker threads created
n_wrk_failed                0         0.00 N worker threads not created
n_wrk_max                   0         0.00 N worker threads limited
n_wrk_queue                 0         0.00 N queued work requests
n_wrk_overflow              0         0.00 N overflowed work requests
n_wrk_drop                  0         0.00 N dropped work requests
n_expired                   0          .   N expired objects
n_deathrow                  0          .   N objects on deathrow
losthdr                     0         0.00 HTTP header overflows
n_objsendfile               0         0.00 Objects sent with sendfile
n_objwrite                  0         0.00 Objects sent with write
s_sess                      0         0.00 Total Sessions
s_req                       0         0.00 Total Requests
s_pipe                      0         0.00 Total pipe
s_pass                      0         0.00 Total pass
s_fetch                     0         0.00 Total fetch
s_hdrbytes                  0         0.00 Total header bytes
s_bodybytes                 0         0.00 Total body bytes
sess_closed                 0         0.00 Session Closed
sess_pipeline               0         0.00 Session Pipeline
sess_readahead              0         0.00 Session Read Ahead
sess_herd                   0         0.00 Session herd
shm_records              3480         0.67 SHM records
shm_writes               3480         0.67 SHM writes
shm_cont                    0         0.00 SHM MTX contention
sm_nreq                     0         0.00 allocator requests
sm_nobj                     0          .   outstanding allocations
sm_balloc                   0          .   bytes allocated
sm_bfree             10240000          .   bytes free
backend_req                 0         0.00 Backend requests made</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fshahidz.com%2Fvarnish-configuration-and-settings%2F&amp;linkname=Varnish%20Configuration%20and%20Settings"><img src="http://shahidz.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://shahidz.com/varnish-configuration-and-settings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
