Nginx : Password protect a directory/folder
Problem :
You want to protect a directory that only certain individuals with the right password can access via a web browser. How to password protect a directory or folder with Nginx?
Solution :
First you need to create a .htpasswd
file (or other name with a period prefix) and place the file inside the directory.
For example :
/var/www/domain.com/protected/.htpasswd;
Next, generate a password and associate the password to a username. The easiest way is to use the htpasswd
command :
htpasswd -c /var/www/domain.com/protected/.htpasswd username
Or if you prefer to edit the file manually, the .htpasswd
file has the format of :
username:encrypted-password:comment
You can generate the encrypted-password
part with :
http://www.htaccesstools.com/htpasswd-generator/
or with perl command
perl -le 'print crypt("encrypted-password", "salt-hash")'
and typically a .htpasswd
file looks like this :
admin:$apr1$J5kod3nw$B9JK4Wb4xKZly.oC9k5uP0
password for user admin.
Note : the comment part is optional.
Once you have place the .htpasswd
file in the directory that you want to protect. It is time to tell Nginx that the directory is only accessible with the right password.
In the nginx.conf file, under the server block, make the following modification and REMEMBER to modify the server block in HTTPS/SSL part as well if you have one.
server {
listen 80;
server_name domain.com www.domain.com;
location / {
# your other stuff
}
# This will deny access to any hidden file (the file with a .period prefix)
# so that your password will not be accessible from the web browser
location ~ /\. { deny all; }
location /protected {
auth_basic "Entering protected zone. You need to login first.";
auth_basic_user_file /var/www/domain.com/protected/.htpasswd;
}
}
Save the modification to nginx.conf file and restart the Nginx server. Test out to see if everything is working correctly by visiting the protected directory via a web browser and if it is setup correctly, you should see a prompt asking you to enter username and password.
See also : Restart Apache or Nginx web server without password prompt
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8k Golang : HttpRouter multiplexer routing example
+7.7k Golang : Gomobile init produce "iphoneos" cannot be located error
+13.5k Golang : How to determine if a year is leap year?
+12.4k Golang : Remove or trim extra comma from CSV
+8k Golang : Get final or effective URL with Request.URL example
+19.7k Golang : How to get time from unix nano example
+9.2k Golang : Qt Yes No and Quit message box example
+19.5k Golang : Archive directory with tar and gzip
+7.1k Linux : How to fix Brother HL-1110 printing blank page problem
+15.1k Golang : Delete certain files in a directory
+29.6k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+5.6k Golang : Markov chains to predict probability of next state example