{"id":13791,"date":"2019-04-06T08:00:47","date_gmt":"2019-04-06T08:00:47","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13791"},"modified":"2019-04-06T08:00:47","modified_gmt":"2019-04-06T08:00:47","slug":"how-to-password-protect-web-directories-in-nginx","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/how-to-password-protect-web-directories-in-nginx\/","title":{"rendered":"How to Password Protect Web Directories in Nginx"},"content":{"rendered":"<p>Managers of web projects often need to protect their work one way or another. Often people ask how to password protect their\u00a0website while it is still in development.<\/p>\n<div id=\"attachment_17217\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Nginx-Password-Protect-Website.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17217\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Nginx-Password-Protect-Website.png\" alt=\"Nginx Password Protect Website\" width=\"720\" height=\"345\" aria-describedby=\"caption-attachment-17217\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17217\" class=\"wp-caption-text\">Nginx Password Protect Web Directory<\/p>\n<\/div>\n<p>In this tutorial, we are going to show you a simple, but effective technique how to\u00a0password protected web directory when running Nginx as web server.<\/p>\n<p>In case you are using Apache web server, you can check our guide for password protecting a web directory:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/password-protect-apache-web-directories-using-htaccess\/\">Password Protect Web Directories in Apache<\/a><\/li>\n<\/ol>\n<h4>Requirements<\/h4>\n<p>To complete the steps in this tutorial, you will need to have:<\/p>\n<ul>\n<li>Nginx web server installed<\/li>\n<li>Root access to the server<\/li>\n<\/ul>\n<h3>Step 1: Create User and Password<\/h3>\n<p><strong>1.<\/strong>\u00a0To password protect our web directory, we will need to create the file that will contain our encrypted\u00a0<strong>username<\/strong>\u00a0and\u00a0<strong>password<\/strong>.<\/p>\n<p>When using\u00a0<strong>Apache<\/strong>, you can use the \u201c<strong>htpasswd<\/strong>\u201d utility. If you have that utility installed on your system, you can use this command to generate the password file:<\/p>\n<pre># htpasswd -c \/path\/to\/file\/.htpasswd username\r\n<\/pre>\n<p>When running this command, you will be asked to set a password for the above user and after that the\u00a0<strong>.htpasswd<\/strong>\u00a0file will be created in the specified directory.<\/p>\n<div id=\"attachment_17202\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Create-Htpassword.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17202\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Create-Htpassword.gif\" alt=\"Create Nginx User Password File\" width=\"720\" height=\"465\" aria-describedby=\"caption-attachment-17202\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17202\" class=\"wp-caption-text\">htpasswd: Create Nginx User Password File<\/p>\n<\/div>\n<p><strong>2.<\/strong>\u00a0If you don\u2019t have that tool installed, you can create the\u00a0<strong>.htpasswd<\/strong>\u00a0file manually. The file should have the following syntax:<\/p>\n<pre>username:encrypted-password:comment<\/pre>\n<p>The\u00a0<strong>username<\/strong>\u00a0that you will use depends on you, choose whatever you like.<\/p>\n<p>The more important part is the way that you will generate the password for that user.<\/p>\n<h3>Step 2: Generate Encrypted Password<\/h3>\n<p><strong>3.<\/strong>\u00a0To generate the password,\u00a0use Perl\u2019s integrated\u00a0<strong>\u201ccrypt\u201d<\/strong>\u00a0function.<\/p>\n<p>Here is an example of that command:<\/p>\n<pre># perl -le 'print crypt(\"your-password\", \"salt-hash\")'\r\n<\/pre>\n<p>A real life example:<\/p>\n<pre># perl -le 'print crypt(\"#12Dfsaa$fa\", \"1xzcq\")'\r\n<\/pre>\n<div id=\"attachment_17210\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Generate-Encrypted-Pasword.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17210\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Generate-Encrypted-Pasword.gif\" alt=\"Generate Encrypted Pasword\" width=\"720\" height=\"400\" aria-describedby=\"caption-attachment-17210\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17210\" class=\"wp-caption-text\">Generate Encrypted Pasword<\/p>\n<\/div>\n<p>Now open a file and put your username and the generated in string it, separated with semicolon.<\/p>\n<p>Here is how:<\/p>\n<pre># vi \/home\/tecmint\/.htpasswd\r\n<\/pre>\n<p>Put your username and password. In my case it looks like this:<\/p>\n<pre>tecmint:1xV2Rdw7Q6MK.\r\n<\/pre>\n<p>Save the file by hitting\u00a0<strong>\u201cEsc\u201d<\/strong>\u00a0followed by\u00a0<strong>\u201c:wq\u201d<\/strong>.<\/p>\n<div id=\"attachment_17212\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Add-Encrypted-Password-to-File.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17212\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Add-Encrypted-Password-to-File.gif\" alt=\"Add Encrypted Password to htpasswd\" width=\"780\" height=\"434\" aria-describedby=\"caption-attachment-17212\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17212\" class=\"wp-caption-text\">Add Encrypted Password to htpasswd<\/p>\n<\/div>\n<h3>Step 3: Update Nginx Configuration<\/h3>\n<p><strong>4.<\/strong>\u00a0Now open and edit the Nginx configuration file associated with the site you are working on. In our case we will use the default file at:<\/p>\n<pre># vi \/etc\/nginx\/conf.d\/default.conf       [For <strong>CentOS<\/strong> based systems]\r\nOR\r\n# vi \/etc\/nginx\/nginx.conf                [For <strong>CentOS<\/strong> based systems]\r\n\r\n\r\n# vi \/etc\/nginx\/sites-enabled\/default     [For <strong>Debian<\/strong> based systems]\r\n<\/pre>\n<p>In our example, we will password protect the directory root for nginx, which is:\u00a0<code>\/usr\/share\/nginx\/html<\/code>.<\/p>\n<p><strong>5.<\/strong>\u00a0Now add the following two lines section under the path you wish to protect.<\/p>\n<pre>auth_basic \"Administrator Login\";\r\nauth_basic_user_file \/home\/tecmint\/.htpasswd;\r\n<\/pre>\n<div id=\"attachment_17214\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Password-Protect-Nginx-Directory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17214\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Password-Protect-Nginx-Directory.png\" alt=\"Password Protect Nginx Directory\" width=\"720\" height=\"400\" aria-describedby=\"caption-attachment-17214\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17214\" class=\"wp-caption-text\">Password Protect Nginx Directory<\/p>\n<\/div>\n<p>Now save the file and restart Nginx with:<\/p>\n<pre># systemctl restart nginx\r\nOR\r\n# service nginx restart\r\n<\/pre>\n<p><strong>6.<\/strong>\u00a0Now\u00a0copy\/paste that IP address in your browser and you should be asked for password:<\/p>\n<div id=\"attachment_17215\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Nginx-Password-Protect-Login.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17215\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Nginx-Password-Protect-Login.png\" alt=\"Nginx Password Protect Login\" width=\"840\" height=\"578\" aria-describedby=\"caption-attachment-17215\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17215\" class=\"wp-caption-text\">Nginx Password Protect Login<\/p>\n<\/div>\n<p>That\u2019s it! Your main web directory is now protected. When you want to remove the password protection on the site, simply remove the two lines that you just added to\u00a0<strong>.htpasswd<\/strong>\u00a0file or use the following command to remove the added user from a password file.<\/p>\n<pre># htpasswd -D \/path\/to\/file\/.htpasswd username<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/password-protect-web-directories-in-nginx\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managers of web projects often need to protect their work one way or another. Often people ask how to password protect their\u00a0website while it is still in development. Nginx Password Protect Web Directory In this tutorial, we are going to show you a simple, but effective technique how to\u00a0password protected web directory when running Nginx &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/how-to-password-protect-web-directories-in-nginx\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Password Protect Web Directories in Nginx&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13791","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13791"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13791\/revisions"}],"predecessor-version":[{"id":13795,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13791\/revisions\/13795"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}