Skip to content

Instantly share code, notes, and snippets.

@akoebbe
Last active August 29, 2015 14:18
Show Gist options
  • Save akoebbe/80735d0e84852cf5b924 to your computer and use it in GitHub Desktop.
Save akoebbe/80735d0e84852cf5b924 to your computer and use it in GitHub Desktop.
Varnish on Nginx Sandwich Config
// Varnish config for my SSL Nginx -> Varnish -> Nginx -> PHP-FPM stack
acl upstream_proxy {
"127.0.0.1";
}
backend default {
.host = "127.0.0.1";
.port = "8000";
}
sub vcl_recv {
# Set the X-Forwarded-For header so the backend can see the original
# IP address. If one is already set by an upstream proxy, we'll just re-use that.
if (client.ip ~ upstream_proxy && req.http.X-Forwarded-For) {
set req.http.X-Forwarded-For = req.http.X-Forwarded-For;
} else {
set req.http.X-Forwarded-For = regsub(client.ip, ":.*", "");
}
// Remove all cookies except the SSESS* and JSESSION* cookies for logged in users
if (req.http.Cookie) {
set req.http.Cookie = ";" + req.http.Cookie;
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
set req.http.Cookie = regsuball(req.http.Cookie, ";(SSESS.*?|JSESSIONID.*?)=", "; \1=");
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
if (req.http.Cookie == "") {
remove req.http.Cookie;
}
}
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment