auth_basic
The `auth_basic` directive enables basic authentication for a specified context in NGINX.
Description
The auth_basic directive allows for the protection of locations or resources within your NGINX server by requiring HTTP Basic Authentication from clients. When this directive is enabled, clients must provide a valid username and password, which are checked against the credentials set in the configuration. This directive is mainly used in scenarios where simple access control is necessary, such as restricting access to a specific part of your website.
The directive accepts a single argument, which is the realm name. This name is an important part of the authentication process as it will be displayed in the authentication dialog presented to the user by their browser. To fully implement basic authentication, you should also use the auth_basic_user_file directive in conjunction with auth_basic to specify the location of the password file containing the allowed usernames and hashed passwords. Without this, the directive will not be functional as the server won’t know which credentials to validate.
In more advanced setups, you might want to control access further using other directives such as allow and deny, which can be combined with auth_basic to refine which users can access protected content depending on their IP addresses or other criteria.
Config Example
location /private {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}Ensure the password file is correctly created and has appropriate permissions for the NGINX user.
Be careful with syntax; the realm name needs to be quoted, and the directive must end with a semicolon.
Remember to restart NGINX to apply changes after editing the configuration.