Set Up HTTP Authentication With Nginx On Amazon Linux

Setting Up Nginx VHost (Amazon Linux)

yum install nginx 

vim /etc/nginx/conf.d/virtual.conf

Inside virtual.conf

server {
listen 80;
server_name unnitest.com;

root /var/www/unni;
index index.html;
}

Put Entry on /etc/hosts file in local computer as

1.2.3.4 unnitest.com

Now Nginx server is setup.

    HTTP Authentication

Install htpasswd

yum install httpd

Create User and Password
Create a .htpasswd file under your website directory being served by nginx. The following command would create the file and also add the user and an encrypted password to it.

sudo htpasswd -c /var/www/unni/.htpasswd abcuser

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:

login:password

Note that this htpasswd should be accessible by the user-account that is running Nginx.

Update Nginx configuration

#vim /etc/nginx/conf.d/virtual.conf 
server {
        listen 80;
        server_name unnitest.com;

        root /var/www/unni;
        index index.html;
        auth_basic "Restricted";                        #For Basic Auth
        auth_basic_user_file /var/www/unni/.htpasswd;   #For Basic Auth
}

Reload Nginx

sudo /etc/init.d/nginx reload
* Reloading nginx configuration...

DONE 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s