Skip to content

Instantly share code, notes, and snippets.

@alekseyl
Last active February 21, 2017 11:52
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 alekseyl/c6c4d053e714eae5b93b460c3268661f to your computer and use it in GitHub Desktop.
Save alekseyl/c6c4d053e714eae5b93b460c3268661f to your computer and use it in GitHub Desktop.
Conditional location selection with try_files. It may be useful in cases of A\B version of a site running on same domain name. You can do sticky cookie with this example.
server {
set $versioned_location @version_one;
if ( $cookie_condition ) {
set $versioned_location @version_two;
}
if ( $cookie_site_version = "version_one" ) {
set $versioned_location @version_one;
}
location / {
try_files $uri $versioned_location;
}
# sticky cookie example
location @version_one {
add_header Set-Cookie site_version=version_one;
}
location @version_two {
}
# also if you have other locations configs, you need to rewrite them too
# for example for /assets location you may rewrite it to:
location /assets {
try_files $uri $branched_location; # <----difference
expires 1y;
gzip_static on;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment