Configure Basic AUTH with Nginx Web Server on Ubuntu 22.04 LTS

Nginx Basic Authentication (often referred to as Nginx HTTP Basic Authentication or Nginx Auth) is a simple authentication mechanism provided by the Nginx web server. It adds a layer of security to your web server by requiring users to enter a username and password before accessing certain resources.

Prerequisites

  • Up and running ubuntu 22.04 LTS machine.
  • Basic knowledge in Linux commands.
  • Internet connectivity.

In this post, We will show you how to install and configure Basic AUTH with Nginx Web Server on Ubuntu 22.04 LTS

Step 1: Create a Password File

Here’s a simple example of how you might configure Nginx Basic Authentication:

sudo apt-get install apache2-utils -y
sudo htpasswd -c /etc/nginx/.htpasswd your_username

This command creates a password file (/etc/nginx/.htpasswd) and adds the specified username (your_username). You will be prompted to enter and confirm the password.

Step 2: Configure Nginx

Edit your Nginx configuration file (e.g., /etc/nginx/sites-available/default or a custom configuration file):

server {
    listen 80;
    server_name example.com;

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

        # Your other configurations here
    }

    # Additional configurations if needed
}

The auth_basic directive sets the authentication realm (a message that is displayed to users when prompted for credentials).

The auth_basic_user_file directive specifies the path to the password file created earlier.

Step 3: Test and Restart Nginx

Test the Nginx configuration for syntax errors:

sudo nginx -t

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

sudo systemctl restart nginx

Now, when users try to access the protected resource on your website, they will be prompted to enter the username and password you specified. This basic authentication adds an additional layer of security, but it’s essential to use it in conjunction with other security measures and consider using HTTPS to encrypt the credentials during transmission.

Conclusion

We have successfully Installed and configure nginx basic auth configure on ubuntu 22.04 LTS Debian machine, If you still have questions, please post them in the comments section below.

Author

Configure Basic AUTH with Nginx Web Server on Ubuntu 22.04 LTS

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top