All Case Studies Design Development Interviews Our Way Project Management

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 own home folder.

$ htpasswd -c /home/PROJECT_NAME/htpasswd PROJECT_NAME
You will be asked for a password twice.

 

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 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 example 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
Once it's done, you should be all set. When you go to the site you just configured, you should get a modal like this one:

 

Screen_Shot_2015-09-30_at_13.08.38.png

Type in the PROJECT_NAME as your username and the password you configured, and you will be let in.

Follow Netguru
Join our Newsletter

READ ALSO FROM NGINX
Read also
Need a successful project?
Estimate project or contact us