70 likes | 82 Views
Sometimes you may need to secure directories and files in NGINX. Here is how to password protect directory in NGINX.<br><br>Visit https://ubiq.co/tech-blog/how-to-password-protect-directory-in-nginx/
E N D
Install Apache Utils We need to use htpasswd utility to password protect files and directories in NGINX. So 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. Replace developer below with your choice of username. # 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.
Open NGINX configuration file Open terminal and run the following command to open NGINX server configuration file. $ sudo vi /etc/nginx/nginx.conf If you have configured separate virtual hosts for your website (e.g www.example.com), such as /etc/nginx/sites-enabled/website.conf then open its configuration with the following command $ sudo vi /etc/nginx/sites-enabled/website.conf Alternatively, you can also open the default virtual host configuration file. $ sudo vi /etc/nginx/sites-enabled/default
Password Protect NGINX In order to password protect your directory, or certain web pages or even your entire website, 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. 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
Thank You Visit for details https://ubiq.co/tech-blog/how-to-password-protect-directory-in-nginx/