Created
July 13, 2013 02:34
-
-
Save surjikal/5989173 to your computer and use it in GitHub Desktop.
Nginx - Only apply basic auth if htaccess file exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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