Set up Password Authentication with NGINX
Learn how to set up a basic HTTP authentication system. This will allow you to password protect particular directories on your website, so that they can only be accessed after logging in with a username and password.
This form of password protection is a fast and easy way to provide a basic level of security via access authorization to a website.
Requirements
- A server running Linux (Ubuntu 16.04 or CentOS 7)
- NGINX installed and running.
Thanks to free starting credit, you can test the IONOS cloud server for 1 month free of charge (or until the credit is used up) and experience the perfect combination of performance and security!
Create the Password File with OpenSSH
Your server will most likely already have OpenSSH installed. If not, you can install it with the commands:
CentOS 7:
Ubuntu 16.04:
Use the following command to create a file named .htpasswd in the /etc/nginx directory:
For example, to add the user jdoe the command is:
Then add a password for this user with the command:
You will be prompted to enter the password twice to confirm it.
- Packed with great features
- Easy — transfer from any provider
- No transfer fee
Set Up Password Authentication in NGINX
The next step is to add the password authentication directives to the NGINX configuration file for the domain on which you are installing Joomla.
In most cases, the domain's configuration file will be located in /etc/nginx/conf.d on CentoS 7, and /etc/nginx/sites-available on Ubuntu 16.04.
Edit this file:
CentOS 7:
Ubuntu 16.04:
You will add the following lines to this file:
These two directives will provide a basic password restriction to the specified directory. NGINX offers many ways you can set up password protection for various files and directories, including the ability to filter by file type. If you want to set up a more complicated system for password-protection, consult the official NGINX documentation for more details.
To protect the entire site, put the directives inside the existing location / command block. If no command block exists, add one:
If you wish to password-protect a sub-directory, specify that directory instead:
Be sure to add this location block inside the server block. For example, if your configuration file looks like this:
After adding the new section, it will look like this:
Save and exit the file, then restart NGINX:
To test the configuration, visit the URL in a browser. If the configuration is set up correctly, you will see a pop-up message asking you to enter the username and password.
- Secures data transfers
- Avoids browser warnings
- Improves your Google ranking