How to enable htpassword authentication in Nginx

Inorder to protect web directories and files, we can enable htpassword authentication by configuring .htaccess file. But this will not be possible if we are using Nginx webserver as it does not support .htaccess file and its rules.

In this case we need to modify Nginx virtualhost to enable htpassword authentication.

Step1: Open domain virtualhost file and add the following lines along with the existing lines in the server directive.

server {

    location /path {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/htpasswd;

Here we need to replace 'path' with the location that we need to protect with authentication.

Step2: Add the username and password to the htpasswd file in the htpasswd generated format.

Step3: Reload Nginx service

#service nginx reload

That's all…

Leave a Reply