Simple Password Protection with basic_auth
Have simple password protection for your nginx website by using basic_auth.
Setting it up
This method is tested on Debian 12.
-
Install nginx and apache2-utils
# apt install nginx apache2-utils -y
-
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 later, omit the-c
parameter. -
Edit the server config file
Edit your nginx 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; } }
-
Testing the configuration
# nginx -t
If the test was successful, open the IPv4 address of your server in a browser.
A login prompt should appear. Only after entering the correct username and password will you be able to access the website content.