Skip to content

Instantly share code, notes, and snippets.

@Gazer
Last active October 20, 2021 14:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Gazer/5879350 to your computer and use it in GitHub Desktop.
Save Gazer/5879350 to your computer and use it in GitHub Desktop.
Nginx example to redirect users to a "Comming soon portal" if they are not developers.
server {
listen 80;
server_name localhost;
location @until {
# Static "comming soon" directory with index.html and assets
root /var/www/;
index index.html;
}
location /let-me-in {
# Set a cookie to allow this user see the real site!
add_header Set-Cookie dev_access=1;
return 301 http://localhost/;
}
location / {
# If HTTP Error 418 is raised, we "render" @until location
# 418 is the code for "internal redirection" used by nginx
error_page 418 = @until;
recursive_error_pages on;
# Tricky part. Check cookie and deny access if not set
set $developer N;
if ($http_cookie ~* "dev_access") {
set $developer S;
}
# No cookie, so no access. Redirect to "@until"
if ($developer = N) {
return 418;
}
# Normal site, let the user see it
root /path/to/rails/app/public;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:3000/;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment