How to Protect Your Ruby on Rails Staging App with Password

There are situations when you are in need of password protection for staging apps.
For example, you need to show something only to your client. In this quick tutorial, I will show you how to password protect your application in Nginx using htpasswd
Step 1
Generate the htpasswd file. I assume your application is placed in its home folder.
$ htpasswd -c /home/PROJECT_NAME/htpasswd PROJECT_NAME
The file you generated will store the user login (PROJECT_NAME in our case) and a hash of the password, so it's not retrievable.
If you need to reset the password, you can run the command again with the existing file:
$ htpasswd /home/PROJECT_NAME/htpasswd PROJECT_NAME
Note that the -c switch is no longer used.
Step 2
Configure Nginx to use the htpasswd file.
Here's an exemplary Nginx configuration:
server {
…
location / {
auth_basic "Restricted";
auth_basic_user_file /home/PROJECT_NAME/htpasswd;
…
}
}
Step 3
Once you have made changes to the Nginx configuration, you need to reload it. The command you need to run is as follows:
$ sudo /etc/init.d/nginx reload
Type in the PROJECT_NAME as your username and the password you configured and you will be let in.