How to add basic authentication to an nginx site?
Published on Aug. 22, 2023, 12:19 p.m.
To add Basic Authentication to an Nginx site, you can follow these steps:
- Create a password file that contains the username and password for each user that needs access to the site:
sudo htpasswd -c /etc/nginx/.htpasswd username
Replace “username” with the desired username. Htpasswd will prompt you to enter a password.
2. Open the Nginx configuration file for your site:
sudo nano /etc/nginx/sites-available/example.com
- Inside the
server
block, add the following directives to the location that needs to be protected:
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
- Reload the Nginx configuration for the changes to take effect:
sudo service nginx reload
With these steps, users will now need to enter a username and password to access the protected location of your site.