Overview
You can password-protect directories on a website running on an Nginx server by creating a basic_auth.conf file. This article explains how to create that file and confirm it's working in a browser.
How this configuration differs from an Apache server
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 use a basic_auth.conf file instead.
Prerequisites
This article assumes you have Created a shell user and are able to log in to your server via SSH.
How do I configure a password-protected directory?
The following steps explain how to create a configuration file and confirm it's working in a browser.
DreamPress plans do not support changes to the local Nginx configuration directory, meaning configuration adjustments cannot be made directly by the user.
If you need to customize the basic_auth.conf file, please contact support for assistance.
Log in to your server via SSH.
Navigate to your user's directory.
View the Nginx configuration file locations article to create your local /nginx/example.com directory.
In this /home/username/nginx/example.com directory, create a file named basic_auth.conf with the following:
location / { auth_basic "Restricted"; auth_basic_user_file /home/username/nginx/example.com/.htpasswd; }usernamewould be your Shell user and example.com your website.The
auth_basicparameter is just the title of the prompt the user sees when visiting this directory.The
auth_basic_user_fileparameter specifies where the password file is. Note how its path is set to the /nginx directory.
In this example, the
locationdirective password protects the entire domain since it's pointing to/.If you want a subdirectory to be password-protected, change the
locationdirective as follows:
location /subdirectory/
Run the following to create the .htpasswd file.
LOGINis the username you want to use to authenticate in the login prompt. Change this to anything you like.[server]$ htpasswd -c /home/username/nginx/example.com/.htpasswd LOGIN
After typing that command, enter a password and confirm it when prompted:
New password: Re-type new password: Adding password for user LOGIN
In your browser, load the directory your /home/username/nginx/example.com/basic_auth.conf points to. In the example above, this would be your domain's root directory since the
locationdirective points to/.Enter a user/password when prompted to log in. In this example, your username is
LOGINand the password is the one you created above.