VPS business hosting starting at $29.95/24/7 premium technical support

Basic Authentication in nginx for any domain, server or location block

sudo yum install httpd-tools
sudo htpasswd -c /etc/nginx/conf.d/vhosts/.htpasswd USERNAME
Enter Password: xxxxx
sudo chmod 644 /etc/nginx/conf.d/vhosts/.htpasswd

Update the domain.conf file like /etc/nginx/conf.d/vhosts/myrestricteddomain.com.ssl.conf

Ideally, use it in server config

server {
satisfy all; 
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/conf.d/vhosts/.htpasswd; 
	location / {

Save and restart nginx

Author: Harshvardhan Malpani

PHP Developer based in New Delhi, India. Working as a freelance web developer providing server deployment, website development and maintenance services.

Leave a Reply

Your email address will not be published. Required fields are marked *