BytesOfProgress

Wiki


Password protect website (basic auth)

Have simple password protection for your nginx website by using basic_auth.

Setting it up

This method is tested on Debian 12.

Step 1: Install nginx and apache2-utils


  # apt install nginx apache2-utils -y
    

Step 2: Set up the password and user: Replace "username" with the name you want to use for the login.


  # htpasswd -c /etc/nginx/.htpasswd username
    

The -c parameter creates the file. If you want to add more users, remove the "c-".

Step 3: Edit the server config file, located at "/etc/nginx/sites-available/default".

  server {
    listen 80;
    server_name WEBSERVER;

    root /var/www/html;
    index index.html;

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

Step 4: Testing the configuration:


  # nginx -t
    

If the test was successful, open the IPv4 address in your browser. There should pop up a prompt for your specified user and password. We will only be able to see the websites content after a successful login.




back