Gaming industry under DDoS attack. Get DDoS protection now. Start onboarding
  1. Home
  2. Developers
  3. How to Configure Basic Authentication in NGINX

How to Configure Basic Authentication in NGINX

  • By Gcore
  • September 1, 2023
  • 2 min read
How to Configure Basic Authentication in NGINX

Configuring basic authentication in NGINX is an essential step for anyone looking to add an extra layer of security to their web pages. By restricting access to authorized users, you can ensure your content remains exclusive and your server stays protected. This guide will walk you through the straightforward process, ensuring you’re well-equipped to fortify your NGINX setup.

Setting up Basic Authentication in NGINX

Setting up Basic Authentication in NGINX is a fundamental security measure to restrict unauthorized access to your web server’s specific areas. By prompting users for a username and password, you ensure that only authorized personnel can access certain resources. Here are step-by-step instructions, complete with descriptions, inputs, and expected outputs:

#1 Install httpd-tools

This tool provides the htpasswd utility, which we’ll use to create a password file by running this command:

sudo apt-get install apache2-utils

#2 Create a Password File

Using htpasswd, create a password file. The -c option is used only when creating a new file.

sudo htpasswd -c /path/to/.htpasswd username

Replace ‘username’ with the desired username you’re working with. You’ll be prompted to enter and confirm your password. Replace ‘/path/to/’ with the actual path where you intend to store your password file. While ‘.htpasswd’ is a commonly used name for this file, you can rename it if you prefer.

Once you run the command, the output should look like this:

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

Make sure to type your password slowly and carefully to prevent any mistakes. This approach can help ensure accuracy and avoid potential access issues.

#3 Configure NGINX for Basic Authentication

Modify your NGINX configuration file to reference the password file. Open your NGINX configuration:

sudo nano /etc/nginx/sites-available/default

Add or modify the location block you wish to protect. For instance:

location /protected/ {    auth_basic "Administrator Login";    auth_basic_user_file /etc/nginx/.htpasswd;}

Once you’re done, save by pressing ‘CTRL + O’, and then press ‘Enter’ to confirm. To exit the editor, press ‘CTRL + X’.

#4 Reload NGINX

Apply the changes by reloading NGINX.

sudo systemctl reload nginx

#5 Test Basic Authentication

Navigate to the protected location in your web browser. As a result, a login prompt will appear, asking for the username and password. After entering the correct credentials, you should be able to access the resource. Entering incorrect credentials will result in an authorization error.

That’s all! With these steps, you’ve acquired the knowledge to set up basic authentication in NGINX. This added layer of security ensures that only authorized users can access specific parts of your website, enhancing its protection against unauthorized access. Remember to always use strong, unique passwords and periodically review your security configurations.

Related articles

What is Function as a Service (FaaS)?

Function as a Service (FaaS) is a serverless computing model that lets developers write and deploy code in small, independent functions without managing any server infrastructure. Over 50% of organizations now adopt serverless technologies

What is DNS-over-HTTPS (DoH)?

DNS-over-HTTPS (DoH) is an internet security protocol that encrypts DNS queries by sending them over HTTPS connections on port 443, the same port used for standard HTTPS traffic. Standardized by the IETF in RFC 8484 in October 2018, DoH pre

TLS 1.3 vs TLS 1.2: what’s the difference?

TLS 1.3 vs 1.2 refers to the comparison between two versions of the Transport Layer Security protocol, a cryptographic standard that encrypts data exchanged between clients and servers to secure network communications. TLS 1.3, finalized in

What does SSL handshake failed mean?

An “SSL handshake failed” error occurs when a client and server can't complete the initial negotiation process required to establish a secure encrypted connection. This negotiation happens before any data transfer begins and typically compl

What is TLS 1.3?

TLS 1.3 is the latest version of the Transport Layer Security protocol, standardized in RFC 8446 in August 2018. This cryptographic protocol secures communication between clients and servers across the internet, from web browsing to API cal

What is DNS load balancing?

DNS load balancing is a technique that distributes incoming network traffic across multiple servers by manipulating DNS responses to improve the availability and performance of applications or services. It works at the application layer (la

Subscribe to our newsletter

Get the latest industry trends, exclusive insights, and Gcore updates delivered straight to your inbox.