HTTP Basic Auth with nginx on CentOS 7

by

The other day I decided to set up a fresh VPS for few of my small apps. Essentially it's a CentOs 7 box with nginx which serves one static site when requests come on one domain (in few different contexts/locations with different settings for each of them) and proxies traffic to a Node app on a different domain. Since CentOS7 doesn't have httpd-tools package installed by default we'll need to install that first: yum -y install httpd-tools Next - use it to generate password. We will want to output this to a .htpasswd file. Assuming username "exampleuser": htpasswd -c /etc/nginx/.htpasswd exampleuser This will prompt you for password which will be later encrypted. Then add these two lines to your nginx config (location or server block): auth_basic "Restricted"; auth_basic_user_file /your-app-path/.htpasswd; Restart nginx to pick up the changes and you should be asked for password when you next try to reach that URL. sudo systemctl restart nginx Extra... more


Tags: http, basic, auth, htpasswd, centos, nginx