Last active
December 18, 2020 20:11
-
-
Save brunoamaral/63d6654910ebadbbad980b78f02ff956 to your computer and use it in GitHub Desktop.
a simple script to password protect a directory on nginx
This file contains hidden or 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
#!/bin/bash | |
INCLUDE="/etc/nginx/incl/basicauth.conf" | |
SOURCEDIR="/home/Digital-Insanity/content/" | |
read -p "Website nginx directory without trailling slash [/var/web/brunoamaral.eu]:" publicDir | |
publicDir=${publicDir:-/var/web/brunoamaral.eu} | |
read -p "Which directory should we protect? (no trailling slash)" relUrl | |
htpasswd -c "$publicDir$relUrl/.htpasswd" nginx; | |
cp "$publicDir$relUrl/.htpasswd" "$SOURCEDIR$relUrl" | |
printf '%s\n' "location $relUrl {" 'try_files $uri $uri/ =404;' 'auth_basic "not allowed";' "auth_basic_user_file $relUrl/.htpasswd;" '}' >> "$INCLUDE" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment