How To Protect Directory With Password on Nginx


Unlike Apache, Nginx does not have any .htaccess file. Password protection is achieved by using the Nginx HttpAuthBasic module directives in the configuration file. For future reference, I will show you a steps to protect directory with password on nginx. This article assumes you have at least basic knowledge of linux, know how to use the shell, and most importantly, you host your site on your own VPS.

Protect Directory With Password on Nginx

Step 1. First add the following to your Nginx configuration file:

location / {
  auth_basic            "Restricted";
  auth_basic_user_file  /etc/nginx/htpasswd;

Step 2. Create the htpasswd file, notice that the file is /etc/nginx/htpasswd. This means you need to use htpasswd to create that file:

htpasswd -c /etc/nginx/htpasswd yourusername
New password: 
Re-type new password: 
Adding password for user yourusername

Step 3. This will create the password file. Next restart nginx’s configuration:

service nginx restart

Now when you visit your directory or domain, you will be asked to enter a username and password that you chose beforehand. This is definitely not the most secure way of restricting domain access.

Congratulation’s! You have successfully protect directory on Nginx. Thanks for using this tutorial for protect directory with password on Nginx system. For additional help or useful information, we recommend you to check the official Nginx web site.

Nginx Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “Nginx Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get a best deal!