How to setup HTTP Authentication with Nginx on Ubuntu

Nginx is an HTTP and reverse proxy server which is lightweight as compare to apache. When we need http authentication for secure our site admin login then there is a need to setup HTTP Authentication with our server. So below steps define how to setup HTTP Authentication with Nginx on Ubuntu Server.

Setup HTTP Authentication with Nginx

Install Apache Utils

First we need to install apache utils which require for generate encrypted password for the user using basic authentication. Install apache2-utils using following command –

linuxtweaks ~]#apt-get install apache2-utils

Create user and password

Create a .htpasswd file under your web root directory being served by nginx virtual host. The following command would use to create htpasswd file –

Nginx Configuration

Next we need to configure nginx for using http authentication for our admin login page.Here we can secure whole site or some of pages of our site.

So update nginx virtualhost under /etc/nginx/sites-enabled/linuxtweaks.conf and add below line of code.if secure whole site which is in development or just to secure. Add two line of code in your nginx vhost configuration so your vhost will look like this.

Above code will call the http authentification, but not executed php as scripts so the browser downloads the wp-login.php, when the credentials have been entered, instead of going to the main login screen.

Reload Nginx

Reload/restart nginx server to take effect of your configuration.

linuxtweaks ~]#service nginx reload

Now you can check if your whole site / selected page is secure through http authentication.

Ablout LinuxTweaks

Welcome to Linuxtweaks – the blog contains a lot of useful information for Linux System administrator ,newbies who are going to start there career as Linux System admin and also for linux desktop users. We basically share what we are getting issue on daily bases.We started this blog on November 2014 because we wanted to keep our knowledge on one place for future uses and also for sharing our knowledge with others on the basic of Linux technologies.