Varnish Load balacing based on Cookie
March 26th, 2012You can load balance with Varnish based on Cookies using the following small hack.
thanks to @kristianlyng , You rock ![]()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
director localhosts {
{ .backend = one; .weight = 1; }
{ .backend = two; .weight = 1; }
{ .backend = { .host = "localhost"; .port = "80"; } .weight = 1; }
}
import std;
sub vcl_recv {
set req.backend = localhosts;
if (req.http.cookie !" "session_id") {
set req.http.x-cookie-id = "session_id=" + std.random();
set req.http.x-cookie-set = "yes";
} else {
set req.http.x-cookie-id = regsub(req.http.cookie, "(session_id=\d)","\1");
}
set client.identity = req.http.x-cookie-id;
}
sub vcl_deliver (
if (req.http.x-cookie-set == "yes") {
set resp.http.set-cookie = req.http.x-cookie-id;
}
} |
Small demo what Varnish is
March 21st, 2012Syntax highlighting VLC ni ViM
March 21st, 2012if you want to edit Varnish VCL and want to see some cool syntax highlighting use the following vim source ![]()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
" Vim syntax file
" Filename: vcl.vim
" Language: Varnish configuation Language, http://www.varnish-cache.org/wiki/VCL
" Maintainer: Elan Ruusamäe <glen@delfi.ee>
" Version Info: $Revision: 1.7 $
" Last Change: $Date: 2010/04/06 08:24:28 $ UTC
" For version 5.x: Clear all syntax items
" For version 6.x: Quit when a syntax file was already loaded
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
" TODO
" - sub ...
" - backend/director/...
" - acl ...
" - error when invalid operator used in if (...)
" - +=, = operators
" - functions
" Code Blocks
" sub NAME {
" backend NAME {
" director NAME FLAGS {
" acl NAME {
"syn region vclCodeBlock start="\s*\<sub\|backend\|director\|acl\>" end="{"he=e-1 contains=vclCodeBlockName,vclFunctionName
"syn match vclCodeBlockName "\<sub\|backend\|director\|acl\>" contained
"syn match vclFunctionName "\h[[:alnum:]_:]*" contained
"syn match vclFunctionName "\h\w*[^:]" contained
"
syn keyword vclOperator set call return error esi synthetic include remove unset
" return modes
syn keyword vclModes deliver pipe pass hash lookup discard fetch restart
" C strings
syn region vclString start=+L\="+ skip=+\\\\\|\\"+ end=+"+ contains=vclSpecial
syn match vclSpecial display contained "\\\(x\x\+\|\o\{1,3}\|.\|$\)"
syn match vclSpecialError "L\='\\[^'\"?\\abfnrtv]'"
syn match vclSpecialCharacter "L\='\\['\"?\\abfnrtv]'"
syn match vclSpecialCharacter display "L\='\\\o\{1,3}'"
syn match vclSpecialCharacter display "'\\x\x\{1,2}'"
syn match vclSpecialCharacter display "L'\\x\x\+'"
syn keyword vclConditional if else elsif elseif
" Numbers
syn match vclNumbers display transparent "\<\d\|\.\d" contains=vclNumber,vclNumberTime
syn match vclNumber display contained "\d\+"
" set obj.ttl = 0s, 0m;
syn match vclNumberTime display contained "\d\+[dhsm]"
" client
syn match vclOption /client\.\(ip\|identity\)/
" server
syn match vclOption /server\.\(ip\|port\)/
" req
syn match vclOption /req\.\(hash\|request\|url\|proto\|backend\.healthy\|backend\|grace\|xid\|restarts\)/
" bereq
syn match vclOption /bereq\.\(request\|url\|proto\|connect_timeout\|first_byte_timeout\|between_bytes_timeout\)/
" beresp
syn match vclOption /beresp\.\(proto\|status\|response\|cacheable\|ttl\|lastuse\|hits\|hash\|grace\|prefetch\|saintmode\)/
" obj
syn match vclOption /obj\.\(proto\|status\|response\|cacheable\|ttl\|lastuse\|hits\|hash\|grace\|prefetch\)/
" resp
syn match vclOption /resp\.\(proto\|status\|response\)/
" common: http.HEADERNAME
syn match vclOption /\(req\|bereq\|resp\|beresp\|obj\)\.http\.[A-Za-z][-_A-Za-z0-9]*/
" Highlight the C block
syn include @vclC syntax/c.vim
unlet b:current_syntax
" Mark block tags itself as comment
syn region vclCBlock matchgroup=vclComment start=/C{/ end=/}C/ contains=@vclC keepend
" Synthetic
syn region vclSynthetic start=/{"/hs=s+2 end=/"}/he=e-2 contains=@vclHTML keepend
" Allow html in synthetic
syn include @vclHTML syntax/html.vim
unlet b:current_syntax
syn match vclComment '#.*'
syn match vclComment "//.*"
syn region vclComment start="/\*" end="\*/"
syn sync ccomment vclComment
hi link vclCodeBlock Function
hi link vclComment Comment
hi link vclStatement Statement
hi link vclFunctionName Identifier
hi link vclCodeBlockName Statement
hi link vclSpecial SpecialChar
hi link vclString String
hi link vclConditional Conditional
hi link vclSynthetic vclString
hi link vclSpecialCharacter vclSpecialSpecial
hi link vclOperator Operator
hi link vclModes Operator
hi link vclOption Identifier
hi link vclNumber Number
hi link vclNumberTime Number |
|
1 |
Either edit your .vimrc file and add |
12 au BufRead,BufNewFile *.vcl :set ft=vclau! Syntax vcl source ~/.vim/syntax/vcl.vim
|
1 2 3 |
or just load it on the fly with:
:set ft=vcl |
change server tag with varnish
March 21st, 2012You can hide or just simple change the default webserver ID tag (and have some fun
)
Make an backend server like apache appear like IIS.
|
1 2 3 4 5 |
sub vcl_fetch {
unset beresp.http.Server;
set beresp.http.Server = "Microsoft-IIS/7.5";
set beresp.grace = 20m;
} |
or be cool and change it to “GWS/2.1″ ![]()
Varnish Training 2012
March 21st, 2012Well me (nossie) and Ebuh are currently attending the Varnish training 2012 held in Paris, France.
