Skip to content

Instantly share code, notes, and snippets.

@ThijsFeryn
Created July 29, 2021 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThijsFeryn/999747408c7a4a6835118a4dc457ee57 to your computer and use it in GitHub Desktop.
Save ThijsFeryn/999747408c7a4a6835118a4dc457ee57 to your computer and use it in GitHub Desktop.
Redirect to the www hostname in Varnish
vcl 4.0;
import std;
sub vcl_recv {
if (!req.http.X-Forwarded-Proto) {
if(std.port(server.ip) == 443) {
set req.http.X-Forwarded-Proto = "https";
} else {
set req.http.X-Forwarded-Proto = "http";
}
}
if (req.http.Host !~ "^www\.") {
return (synth(301, req.http.X-Forwarded-Proto + "://www." + req.http.Host + req.url));
}
}
sub vcl_synth {
if (resp.status == 301 || resp.status == 302) {
set resp.http.Location = resp.reason;
set resp.reason = "Moved";
return (deliver);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment