Created
October 3, 2017 14:13
-
-
Save renizgo/811d3f963bfb4ef5a48b19c6352542e8 to your computer and use it in GitHub Desktop.
Varnish VCL 4 - With Cookies Languages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vcl 4.0; | |
import std; | |
import directors; | |
# probe health { | |
# # Url de consulta, padrao eh "/" | |
# # .url = "/"; | |
# # Especifica uma requisicao completa de HTTP usando multiplas strings | |
# .request = | |
# "HEAD / HTTP/1.1" | |
# "Host: homolog.opac.scielo.org" | |
# "Connection: close" | |
# "User-Agent: Varnish Health Probe"; | |
# # Codigo HTTP esperado. padrao eh 200. | |
# .expected_response = 200; | |
# # Tempo de TimeOut para o Probe | |
# .timeout = 3s; | |
# # Frequencia que o probe faz a checagem | |
# .interval = 30s; | |
# # Quantas pesquisas para testar a saude do backend | |
# .window =5; | |
# # Se 3 tiverem sucesso eu considero o backend saudevel | |
# .threshold = 3; | |
# } | |
backend lb01opac { | |
# .host = "192.168.1.60"; | |
# .port = "56888"; | |
# .probe = health; | |
.host = "192.168.169.100"; | |
.port = "5001"; | |
# .probe = health; | |
} | |
backend lb02opac { | |
# .host = "192.168.1.61"; | |
# .port = "56888"; | |
# .probe = health; | |
.host = "192.168.169.100"; | |
.port = "5001"; | |
# .probe = health; | |
} | |
sub vcl_init { | |
new round_robin_director = directors.round_robin(); | |
round_robin_director.add_backend(lb01opac); | |
round_robin_director.add_backend(lb02opac); | |
} | |
sub identify_user_lang { | |
if (req.http.cookie ~ "language=") { | |
#unset all the cookie from request except language | |
set req.http.X-Language = regsub(req.http.cookie, "(.*?)(language=)([^;]*)(.*)$", "\3"); | |
} | |
} | |
sub vcl_recv { | |
# Regra de X-Forwarded-For | |
if (req.http.x-forwarded-for) { | |
set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip; | |
} else { | |
set req.http.X-Forwarded-For = client.ip; | |
} | |
# Regra geral para fazer cache de requisicoes GET e HEAD | |
if (req.method != "GET" && | |
req.method != "HEAD" && | |
req.method != "PUT" && | |
req.method != "POST" && | |
req.method != "TRACE" && | |
req.method != "OPTIONS" && | |
req.method != "DELETE") { | |
# Non-RFC2616 or CONNECT which is weird. | |
return (pipe); | |
} | |
# Only cache GET or HEAD requests. This makes sure the POST requests are always passed. | |
if (req.method != "GET" && req.method != "HEAD") { | |
return (pass); | |
} | |
if (req.url ~ "^/admin") { | |
return (pass); | |
} | |
call identify_user_lang; | |
# if(!req.http.cookie ~ "language"){ | |
# #unset all the cookie from request except language | |
# unset req.http.Cookie; | |
# } | |
unset req.http.Cookie; | |
return (hash); | |
} | |
# The data on which the hashing will take place | |
sub vcl_hash { | |
# Called after vcl_recv to create a hash value for the request. This is used as a key to look up the object in Varnish. | |
hash_data(req.url); | |
if (req.http.host) { | |
hash_data(req.http.host); | |
} else { | |
hash_data(server.ip); | |
} | |
if (req.http.X-Language) { | |
# add cookie in hash | |
hash_data(req.http.X-Language); | |
} | |
return (lookup); | |
} | |
sub vcl_backend_response { | |
if (bereq.url ~ "^/admin") { | |
return (deliver); | |
} | |
# if (bereq.url ~ "^/set_locale"){ | |
# set beresp.http.X-Language = regsub(bereq.url, "^/set_locale/(.*?)/", "\1"); | |
# set beresp.ttl = 2s; | |
# set beresp.uncacheable = true; | |
# return (deliver); | |
# } | |
if (!beresp.http.Set-Cookie ~ "language") { | |
unset beresp.http.Set-cookie; | |
} | |
if (bereq.url ~ "^/set_locale" && beresp.status == 302) { | |
set beresp.http.Vary = "Cookie"; | |
} | |
unset beresp.http.expires; | |
set beresp.http.cache-control = "max-age=900"; | |
set beresp.ttl = 168h; | |
unset beresp.http.Server; | |
set beresp.http.Server = "nginx"; | |
return (deliver); | |
} | |
sub vcl_deliver { | |
if (obj.hits > 0) { | |
set resp.http.X-Cache = "HIT"; | |
} else { | |
set resp.http.X-Cache = "MISS"; | |
} | |
return (deliver); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment