Skip to content

Instantly share code, notes, and snippets.

@surjikal
Created July 13, 2013 02:34
Show Gist options
  • Save surjikal/5989173 to your computer and use it in GitHub Desktop.
Save surjikal/5989173 to your computer and use it in GitHub Desktop.
Nginx - Only apply basic auth if htaccess file exists
server {
listen 80;
server_name ~^(?<instance>.+?)\.foo.example.com$;
set $instance_root /instances/$instance.foo.example.com;
set $htaccess_user_file /htaccess/$instance.foo.example.com.htaccess;
root $instance_root;
# Abusing unused error codes to perform an internal redirect
# to a named location. This was the advice from nginx gurus.
error_page 599 = @noauth;
if (!-f $htaccess_user_file) {
return 599;
}
location / {
auth_basic "Restricted";
auth_basic_user_file $htaccess_user_file;
try_files $uri /index.html =404;
}
location @noauth {
try_files $uri /index.html =404;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment