November 16, 2019 Network
Password protecting (sub)directories on Nginx without htpasswd
Overview
Managers of web projects often need to protect their work one way or another. On an Apache server, it's possible to password protect a directory using .htaccess
and .htpasswd
files. However, .htaccess
files are not supported on Nginx. You can still password protect your directories, but you need to modify Nginx .conf file instead.
htpasswd
To date, htpasswd
is usually used to generate username and password. That is, we need to install htpasswd
additional if no Apache in Linux. Becausehtpasswd
is used to create and update the flat-files used to store usernames and password for basic authentication of HTTP users.
Alternative for htpasswd
Online Htpasswd Generator, is simple and esay way to get username and password. For example, just click http://www.htaccesstools.com/htpasswd-generator/. Input username and password, and click button create .htpasswd file
to copy result to your auth_basic_user_file
. It's done.
Main codes
location / {
auth_basic "Restricted";
auth_basic_user_file nginx_htpasswd.pl;
}
Tips:
- If you want a subdirectory to be password protected, change the "location /" as "location /subdirectory/".
- "Restricted" can change anything you want and it will show browser dialog to prompt username and password.
- Give path of file "nginx_htpasswd.pl" after "auth_basic_user_file".
That's all.
Nov 11, 2019
References:
- https://help.dreamhost.com/hc/en-us/articles/215837528-Password-protecting-directories-with-Nginx
- http://www.fecmall.com/topic/407