A short howto, create a basic HTTP authentication on Nginx, Centos 6.8.


Requirements
  • Nginx is setup correctly
  • root access
Installation

First lets get htpasswd, this “manages” the password file and creates users.

yum install httpd-tools

 

The first run you need to create a new .htpasswd file :

htpasswd -c /var/www/html/.htpasswd svennd

This would create the passwd file and a user svennd (you will be prompted for the password)

Next the nginx configuration in the server {} block :

location /my_secret {
        auth_basic "restricted";
        auth_basic_user_file /var/www/html/.htpasswd;
}

To restrict access to my_secret directory. Next is to check if nginx configuration is fine :

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

And reload nginx :

service nginx reload

Bam, that easy !