auth_basic_user_file

The auth_basic_user_file directive specifies a file that contains usernames and password hashes for HTTP Basic Authentication.

Syntaxauth_basic_user_file file;
Defaultnone
Contexthttp, server, location, limit_except
Arguments1

Description

The auth_basic_user_file directive is used in NGINX to set the path to a file that contains pairs of usernames and password hashes required for authenticating users through HTTP Basic Authentication. This directive is typically used in combination with the auth_basic directive, which enables Basic Authentication for a defined context.

When the user makes a request to an NGINX server configured with Basic Authentication, the server checks if the auth_basic_user_file directive is present. If so, it reads the specified file to verify the username and password submitted by the client against the stored hashes. The file must be in a specific format, where each line contains a username followed by a password hash, generated using a tool like htpasswd from the Apache HTTP Server suite. The authentication process aims to restrict access to specific resources, ensuring that only valid users can access them.

It's important to ensure that the user file is securely stored and not publicly accessible to prevent unauthorized access to the sensitive credentials contained within. Additionally, NGINX improves security by supporting various cryptographic hashing algorithms through the use of password hashes, making it more difficult for attackers to compromise user accounts.

Config Example

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

The user file must be readable by the NGINX process; ensure correct file permissions are set.

The format of the user file must be strictly followed (username:hashed_password).

If the path to the user file is incorrect, authentication will fail, allowing open access.

← Back to all directives