Create file .htpasswd somewhere outside your site-directory:
sudo htpasswd -c /etc/nginx/.htpasswd exampleusername
The tool will prompt you for a password:
New password: Re-type new password: Adding password for user exampleuser
The structure of the .htpasswd file would be like this:
username:password
Now put (or modify) next block into file /etc/nginx/sites-available/yoursite
location / {
auth_basic "Auth needed";
auth_basic_user_file /etc/nginx/.htpasswd;
}
Restart nginx:
sudo service nginx restart
Thanks to:
http://kbeezie.com/apache-to-nginx/
https://www.digitalocean.com/community/tutorials/how-to-set-up-http-authentication-with-nginx-on-ubuntu-12-10