How to Password Protect an Nginx virtualhost

Note: These instructions were tested on a Joyent Solaris SmartMachine. It should work on all other hosts though.

Modify nginx.conf

Go into your nginx.conf for your server and add:

1
2
3
4
location  /  {
  auth_basic            "Restricted";
  auth_basic_user_file  sitename.htpasswd;
}

Create authentication file

Create the authentication file by running:

1
sudo htpasswd -c sitename.htpasswd user

and it will prompt you for your password for the site.

Reload Nginx

Tell nginx to reload its configuration files:

1
sudo nginx -s reload

You’re done!

Complete Documentation

For more information read the full docs on Nginx’s basic auth module.

blog comments powered by Disqus

Say Hello