80 likes | 95 Views
NGINX basic authentication allows you to protect servers, websites, virtual hosts and web pages. Here is how to configure basic authentication in NGINX. #nginx #security #authentication #apache #webdevelopment <br><br>Visit https://ubiq.co/tech-blog/how-to-configure-basic-authentication-in-nginx/
E N D
Install Apache Utils We need to use htpasswd utility to set up basic authentication. For that, we need to install apache2-utils or httpd-tools. Open terminal and run the following command # yum install httpd-tools [RHEL/CentOS] $ sudo apt install apache2-utils [Debian/Ubuntu]
Create User/Password Next, run htpasswd command to create a user that will be given access to your website. # htpasswd -c /etc/nginx/conf.d/.htpasswd developer We use -c option to specify password file location. When you press enter, you will be prompted for a password. For example, when we create another user, we don’t specify password file location. # htpasswd /etc/nginx/conf.d/.htpasswd developer2
Open NGINX configuration file Open terminal and run the following command to open NGINX server configuration file. $ sudo vi /etc/nginx/nginx.conf
Password Protect NGINX In order to password protect your website, or certain web pages, we need to use auth_basic and auth_basic_user_file directives in NGINX server configuration. For example, if you want to configure basic authentication for virtual hosts (an entire http block), add the above two directives as shown below in http block. You can also add it in server or location blocks. http{ ... auth_basic "Restricted Access!"; auth_basic_user_file /etc/nginx/conf.d/.htpasswd; ... }
Restart NGINX Server Run the following command to check syntax of your updated config file. $ sudo nginx -t If there are no errors, run the following command to restart NGINX server. $ sudo service nginx reload #debian/ubuntu $ systemctl restart nginx #redhat/centos
Verify basic authentication Open browser and visit the URL (e.g www.example.com/admin) that you have protected. You should see an authentication screen asking for username and password.
Thank You Visit for details https://ubiq.co/tech-blog/how-to-configure-basic-authentication-in-nginx/