Skip to content

Instantly share code, notes, and snippets.

@brunoamaral
Last active December 18, 2020 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brunoamaral/63d6654910ebadbbad980b78f02ff956 to your computer and use it in GitHub Desktop.
Save brunoamaral/63d6654910ebadbbad980b78f02ff956 to your computer and use it in GitHub Desktop.
a simple script to password protect a directory on nginx
#!/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