preload preload preload preload

Varnish Configuration and Settings

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 a backend server and caches the returned documents to better satisfy future requests for the same document.

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.

Varnish uses two configuration file one is VCL and another one contains the values which will be passed as parametes to the varnishd.

/etc/default/varnish

# Configuration file for varnish

# Main configuration file. You probably want to change it :)
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

/etc/varnish/vcl.conf

      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" && req.request != "HEAD") {
                 pipe;
             }
             if (req.request == "POST") {
                 pass;
             }
             if (req.request == "GET" && req.url ~ "\.(jpg|jpeg|gif|ico)$") {
                 lookup;
             }
             if (req.request == "GET" && req.url ~ "\.(css|js)$") {
                 lookup;
             }
             if (req.request == "GET" && 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;
         }

You can use varnishstat to see the status of varnish

$ 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
  • Share/Bookmark
  • Leave a Reply

    * Required
    ** Your Email is never shared