Skip to content

Instantly share code, notes, and snippets.

@brycied00d
Created May 29, 2015 09:01
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 brycied00d/627ec6c4235405838247 to your computer and use it in GitHub Desktop.
Save brycied00d/627ec6c4235405838247 to your computer and use it in GitHub Desktop.
Abusing Nginx Configuration To Serve And Redirect Multiple Subdomains Under a Variable Domain
# The idea is that speedtest.companyA.com can be served from the same server{} block as speedtest.companyB.com
# The index page has links to allow the user to force a connection over ipv4 or ipv6. A connection to /ipv4, for instance,
# will redirect the user to ipv4.speedtest.$X/ where $X is determined dynamically. Likewise for /ipv6, and there's /default
# to redirect to just speedtest.$X/
# Note that this does assume that "ipv4.speedtest.$X" and "ipv6.speedtest.$X" are setup with A and AAAA records respectively.
location = /ipv4 {
# IP access requires a hard-coded IPv4 address for the redirect.
if ($host = "[$server_addr]") { return 302 $scheme://$ipv4addr/; }
if ($host = "$server_addr") { return 302 $scheme://$ipv4addr/; }
# A bare "speedtest.____" connection gets ipv4. prepended to the $host
if ($host ~* "^speedtest") { return 302 $scheme://ipv4.$host/; }
# A connection to our ipvX-prepended URL gets that ipvX. replaced with ipv4.
if ($host ~* "^ipv[46].(speedtest\..+)") { return 302 $scheme://ipv4.$1/; }
# Any unhandled patterns get bounced straight back to /
return 302 $scheme://$host/;
}
location = /ipv6 {
# IP access requires a hard-coded IPv6 address for the redirect.
if ($host = $server_addr) { return 302 $scheme://$ipv6addr/; }
# A bare "speedtest.____" connection gets ipv6. prepended to the $host
if ($host ~* "^speedtest") { return 302 $scheme://ipv6.$host/; }
# A connection to our ipvX-prepended URL gets that ipvX. replaced with ipv6.
if ($host ~* "^ipv[46].(speedtest\..+)") { return 302 $scheme://ipv6.$1/; }
# Any unhandled patterns get bounced straight back to /
return 302 $scheme://$host/;
}
location = /default {
if ($host ~* "^ipv[46].(speedtest\..+)") { return 302 $scheme://$1/; }
# All unhandled patterns, including direct IP and "speedtest." just get bounced back to themselves' /
return 302 $scheme://$host/;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment