Setting up Basic Authentication with Nginx

YouTube reference

Get 200$ in Digital Ocean and Get 100$ in Vultr

Setting up Basic Authentication with Nginx :shield:

In the world of web hosting and server management, security is of paramount importance. One of the ways to enhance the security of your web applications and websites is by setting up basic authentication. Basic authentication requires users to enter a username and password before they can access a particular web page or directory. In this guide, we will walk you through the steps to set up basic authentication with Nginx, a popular web server, using necessary Markdown formatting, emojis, and configuration commands.

Prerequisites :clipboard:

Before we dive into the configuration, make sure you have the following prerequisites in place:

  • A server or VPS with Nginx installed and running.
  • Administrative access to your server.

Step 1: Create an Authentication File :closed_lock_with_key:

First, let’s create a password file that will store the usernames and passwords for authentication. We can use the htpasswd command to create this file. If you don’t have htpasswd installed, you can install it using the following command:

sudo apt-get install apache2-utils

Now, create the authentication file with the first user (replace username with your desired username):

sudo htpasswd -c /etc/nginx/.htpasswd username

You will be prompted to enter and confirm the password for the user. This command will create the .htpasswd file in the /etc/nginx/ directory.

Step 2: Configure Nginx :hammer_and_wrench:

Now, let’s configure Nginx to use the authentication file. Open the Nginx configuration file for your website or virtual host. This can typically be found in /etc/nginx/sites-available/ or /etc/nginx/conf.d/.

sudo nano /etc/nginx/sites-available/your-site-config

Add the following lines inside the server block, typically after the server_name directive:

location /private {
    auth_basic "Restricted Access";
    auth_basic_user_file /etc/nginx/.htpasswd;
    # Your other configuration options for this location...
}
  • /private is the URL path you want to protect. You can change this to match the path you want to secure.
  • auth_basic sets the authentication realm (a message users will see when prompted for credentials).
  • auth_basic_user_file points to the .htpasswd file you created in Step 1.

Save and exit the configuration file.

Step 3: Test and Reload Nginx :arrows_counterclockwise:

Test the Nginx configuration for syntax errors:

sudo nginx -t

If the test is successful, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 4: Access the Secured Page :white_check_mark:

Now, when you access the URL path you protected (e.g., http://yourdomain.com/private), you will be prompted to enter the username and password you set up in the .htpasswd file.

Congratulations! You’ve successfully set up basic authentication with Nginx. Your web application or website is now more secure, and unauthorized users will be kept out.

Remember to maintain your .htpasswd file by adding or removing users as needed. With this security measure in place, you can better protect sensitive content on your web server.

:closed_lock_with_key: Stay secure, and happy web hosting! :rocket: