Home   /   DKMIS Blog   /   Codes

Using Password Protection in Nginx

Posted on 31 January 2025 12:13 pm

If you're using Nginx, you can set up basic authentication using a similar approach:

  1. Generate the password file:

printf "username:$(openssl passwd -crypt password)\n" >> /etc/nginx/.htpasswd

           Replace username and password with your credentials 

 

      2.  Edit your Nginx configuration file to include the authentication for the specific URL:

location /protected-url/ {
    auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

            Replace /protected-url/ with the specific path you want to protect.

 

      3.  Reload Nginx:

sudo service nginx reload

 

      4.  Test the URL to verify the password protection is in place.