Skip to main content
In this lesson we implement basic authentication on an NGINX server to protect a single endpoint. The public site will remain open, while an /admin path will require a username and password. Example site:
Open a terminal on the NGINX host and confirm the site is reachable. The generic public page looks like this:
A screenshot of a clean webpage template called "Phantom" with a large headline announcing it's a free, fully responsive HTML5 UP template. Below the header is a grid of colorful square tiles labeled with words like "Magna", "Lorem", and "Feugiat".
At this point, visiting https://www.example.com/admin shows the same public page because authentication isn’t enabled yet. We’ll update the NGINX configuration to require Basic Auth only for the /admin location.

Update the NGINX server configuration

Edit your site configuration (for example /etc/nginx/sites-available/example-https) and add a location /admin block with auth_basic and auth_basic_user_file. This example server block shows a minimal HTTPS configuration with the /admin protection:
Quick reference: what the key directives do Notes:
  • auth_basic is the realm string that appears in the browser prompt (here: "Restricted Access").
  • auth_basic_user_file should point to a readable file containing username:encrypted-password entries.

Create the .htpasswd file and add a user

Create the .htpasswd file and add a user (we’ll add admin in this example). The commands below create or overwrite the file and append an APR1 (Apache MD5) encrypted password produced by openssl passwd:
When prompted enter the desired password (demo uses password123). The .htpasswd file will contain a single line similar to:
Verify the file content:
Ensure the .htpasswd file is readable by the NGINX worker process (adjust ownership or permissions as needed). For example:
Avoid world-writable/readable permissions on sensitive files.

Test and reload NGINX

Validate the configuration and reload NGINX so changes take effect:
Now visit the protected endpoint. Refresh https://www.example.com/admin — the browser should prompt for credentials:
A browser screenshot showing a sign-in dialog box with username and password fields and "Cancel" and "Sign In" buttons near the top center. The address bar displays a kodekloud.dev URL.
Enter the username (admin) and the password you created (e.g., password123). After successful authentication you gain access to /admin. The public / endpoint remains accessible without credentials.

Protecting the entire site

If you prefer to require authentication for the entire site, move the auth_basic and auth_basic_user_file directives into the location / block or the server block (scope depends on your needs). Example replacing the earlier location /:
After editing, run nginx -t and reload NGINX. Note: browsers may cache credentials; use a private/incognito window or clear credentials if you do not see the login prompt immediately.
Basic authentication with .htpasswd is simple and useful for internal or small-scale protection, but it does not scale well for large production deployments. Credentials are sent with every request and managing many users via .htpasswd becomes cumbersome. For production consider more robust solutions like OAuth, OpenID Connect, or integrating with an identity provider or SSO.

Alternatives and integrations

If you use NGINX Plus (commercial) or additional modules, you can integrate NGINX with external identity providers. Examples include the NGINX JavaScript module (njs), OpenID Connect integrations, or vendor-specific modules. Example: install and enable the njs module (package names vary by distribution):
Use the appropriate module and configuration for your chosen identity provider or auth flow.

Summary

This lesson showed how to:
  • Protect a single NGINX location (/admin) using Basic Auth with auth_basic and a .htpasswd file.
  • Create APR1-encrypted credentials using openssl passwd -apr1.
  • Extend protection to the entire site.
  • Consider alternatives for production deployments (OAuth, OpenID Connect, NGINX modules).
Links and references

Watch Video

Practice Lab