Skip to content

Instantly share code, notes, and snippets.

@twilight-sparkle-irl
Created October 31, 2018 16:04
Show Gist options
  • Save twilight-sparkle-irl/6b0d1783580e884fd9d0071cb687d857 to your computer and use it in GitHub Desktop.
Save twilight-sparkle-irl/6b0d1783580e884fd9d0071cb687d857 to your computer and use it in GitHub Desktop.
nginx map location based on cookie (for when you want to test a site but give a static page to everyone else)
# everyone, by default, will go to @underconstruction
# however, if you set cookienamehere=cookievaluehere, you get sent to @testbed instead
# by default, @under_construction shows static files from /var/www/under_construction
# by default, @testbed proxypasses to http://127.0.0.1:3000
# change those to do whatever youw ant
# if you want
# you can change $cookie_cookienamehere to $remote_addr and replace the "~cookievaluehere" with your ip
# that'll make it so that it's based on ip instead of cookie
# i just figured this is better since you can choose to see the construction page if you want
# please note that this is dependent on a file named "__THIS_FILE_SHOULD_NEVER_EXIST__" not existing
# if you create a file with that name idk what your issue is. just dont do that.
# (__THIS_FILE_SHOULD_NEVER_EXIST__ is required because nginx is weird about try_files)
# a nyway heres the code:
map $cookie_cookienamehere $end_to_use {
default @under_construction;
"~cookievaluehere" @testbed;
}
server {
server_name example.com;
index index.html index.htm;
listen 80;
location / {
try_files /__THIS_FILE_SHOULD_NEVER_EXIST__ $end_to_use;
}
location @under_construction {
root /var/www/under_construction;
try_files $uri $uri/ =404;
}
location @testbed {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto https;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment