Knowledge Base
MilesWeb / How-Tos

How to Password Protect Directories with Nginx?

Approx. read time : 2 min

You can password protect a directory on the Apache server using .htaccess and .htpasswd files. But, .htaccess files are not supported on Nginx.

Still, you can password protect your directories by using a basic_auth.conf file instead.

Creating the File

1. Log into your server using SSH.

2. Go to your user’s directory.

3. Check for the /home/username/nginx/example.com directory. This isn’t available by default; you need to create it by running the below command:

[server]$ mkdir -p nginx/example.com

4. In this directory, add a file named ‘basic_auth.conf’ with the below command:

location / {
auth_basic "Restricted";
auth_basic_user_file /home/username/nginx/example.com/.htpasswd;
}
  • The auth_basic parameter is simply the title of the prompt seen by the user when he visits this directory.
  • The auth_basic_user_file parameter showcases the location of the password file. Check how its path is set to the /nginx directory.
Note: In this example, the ‘location’ directive password protects the complete domain as it’s pointing to ‘/’.
For password protections a subdirectory, change the ‘location’ directive as below:

location /subdirectory/

5. Execute the following to create the .htpasswd file:

[server]$ htpasswd -c /home/username/nginx/example.com/.htpasswd LOGIN
  • LOGIN indicates the username for authenticating in the login prompt.

6. After you type that command, enter a password and confirm it when you are asked for:

New password:
Re-type new password:
Adding password for user LOGIN

7. You will need to reload the nginx config file.

8. Load the directory your /home/username/nginx/example.com/basic_auth.conf points to in your browser. *In the example above, this would be your domain’s root directory as the ‘location’ directive points to /.

9. Enter a user/password when asked to log in.

  • In this example, LOGIN is your username and you have the password create in the above step.

This way you can password protect your directing with Nginx.

Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.

Trusted By Thousands of Clients & Big Businesses

We highly appreciate the kind and stellar feedback we receive from our customers. Delivering the best is our goal! MilesWeb is rated Excellent out of 5 based on reviews. Read more reviews.

Excellent
Based on reviews
2 hours ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
4 hours ago
Milesweb is superb Hosting pro...
Milesweb is superb Hosting provider ever, their Support team is amazing!!!...
Abhishek Singh
15 hours ago
Great support in great timing...
We need urgent assistance on changes in a primary domain on our client's Cpanel accounts and reached...
Riyaju Deen
21 hours ago
Best Website Hosting platform ...
I was new on MilesWeb. And needed help on multiple areas from setting up to getting started with cre...
Harshada
1 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
Narendra
1 days ago
the team is very supportive th...
the team is very supportive though at times effort needs to be made to make understand the problem s...
Suree Sharma
1 days ago
I am using miles web for 3plus...
I am using miles web for 3plus years, very quick and perfect support by the team, they helped me man...
Sri Raghav
2 days ago
The service is good...
The service is good. They are answering with patience and doing the needful as soon as possible....
MR
2 days ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
3 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
Narendra
4 days ago
positively helped me with find...
positively helped me with finding insecure content on my website causing SSL to not work properly on...
Thaviraj Junglee
4 days ago
Exceptional support, Truly Pra...
I had opted for the basic wordpress hosting plan as I intended to experiment with various plug-ins. ...
Aseem Chandna