Difference between revisions of "Varnish"

From wiki.mikejung.biz
Jump to navigation Jump to search
(Storage Backends)
(Configuration)
Line 47: Line 47:
 
==Configuration==
 
==Configuration==
  
To configure Varnish using the file backend, do nothing, it's the default. However, you can change the size of RAM to use by changing the "1GB" value to whatever is appropriate.
+
To configure Varnish using the '''file''' backend, do nothing, it's the default. However, you can change the size of RAM to use by changing the "1GB" value to whatever is appropriate.
 
<pre>
 
<pre>
 
vim /etc/sysconfig/varnish
 
vim /etc/sysconfig/varnish
Line 60: Line 60:
 
# # Backend storage specification
 
# # Backend storage specification
 
VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
 
VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
 +
</pre>
 +
 +
To configure Varnish to use the '''malloc''' backend, you will need to comment out the original VARNISH_STORAGE setting, create a new one, specify "malloc,$size_of_cache"
 +
<pre>
 +
# # Backend storage specification
 +
#VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
 +
VARNISH_STORAGE="malloc,256MB"
 
</pre>
 
</pre>
  

Revision as of 21:18, 31 January 2013

Overview

Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 - 1000x, depending on your architecture. A high level overview of what Varnish does can be seen in the video attached to this web page.

Configuration Files

  • Used for parameters and command line arguments. When you change this, you need to run service varnish restart for the changes to take effect.

Debian based

/etc/default/varnish 

Redhat / CentOS

/etc/sysconfig/varnish


  • This contains your VCL and backend-definitions. After changing this, you can run either service varnish reload, which will not restart Varnish, or you can run service varnish restart, which empties the cache.
/etc/varnish/default.vcl
backend default {
   .host = "localhost";
   .port = "8080";
}
  • A backend server is the server providing the content Varnish will accelerate.

.host = The host that varnish will talk to get content. If this is installed on the same server that Apache is using, this would be localhost or 127.0.0.1. If the Apache server is on another server, you would put in the IP of that server, private network is preferable, but a public IP will work as well.

.port = The port that Varnish will connect to to get content to cache. If Apache is listening on port 80, then you would configure this to listen on port 80. If Apache is listening on port 8080, then you would set this to port 8080.

Storage Backends

malloc Varnish will request the entire size of the cache with a malloc call. The OS divides the cache between memory and disk by swapping out what it can't fit into memory.

  • Choose malloc if your cache can fit entirely into RAM.


file Varnish creates a file on the filesystem to contain the entire cache. It then tells the OS via mmap() to map the entire file into memory if possible. This method does not retain data when you stop or start Varnish.

  • Choose file if you have a large cache that will not fit entirely into RAM.

Shared memory log -- Not much needs to be done with this besides making sure the log is stored in RAM (/dev/shm)

Configuration

To configure Varnish using the file backend, do nothing, it's the default. However, you can change the size of RAM to use by changing the "1GB" value to whatever is appropriate.

vim /etc/sysconfig/varnish

# # Cache file location
VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin
#
# # Cache file size: in bytes, optionally using k / M / G / T suffix,
# # or in percentage of available disk space using the % suffix.
VARNISH_STORAGE_SIZE=1GB
#
# # Backend storage specification
VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"

To configure Varnish to use the malloc backend, you will need to comment out the original VARNISH_STORAGE setting, create a new one, specify "malloc,$size_of_cache"

# # Backend storage specification
#VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
VARNISH_STORAGE="malloc,256MB"

Commands and Arguments

Restart Varnish and clear it's cache

service varnish restart

Reloads the vcl file, the cache is not affected

service varnish reload

Shows the cache status and hit ratio

varnishstat

Command line options.

Listen address
-a <[hostname]:port>

Specifies the vcl file location
-f <filename>

Set the tunable parameters
-p <parameter=value>

Authentication secret for management
-S <secretfile>

Management interface
-T <hostname:port>

Where and how to store objects
-s <storagetype,options>

Troubleshooting

nf_conntrack

If you notice some odd issues with Varnish, check dmesg, if you see this you should raise the limit.

# dmesg
nf_conntrack: table full, dropping packet.
nf_conntrack: table full, dropping packet.
nf_conntrack: table full, dropping packet.

To raise the limit:

#See what the current limit is and note it:
cat /proc/sys/net/nf_conntrack_max

vim /etc/sysctl.conf

#Add the following
net.nf_conntrack_max = 100000

#Once that is added, run this to make the change permanent.
sysctl -p