{"id":7333,"date":"2019-06-07T11:29:14","date_gmt":"2019-06-07T11:29:14","guid":{"rendered":"https:\/\/www.milesweb.in\/hosting-faqs\/?p=7333"},"modified":"2019-06-07T11:29:14","modified_gmt":"2019-06-07T11:29:14","slug":"password-protect-directories-with-nginx","status":"publish","type":"post","link":"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/","title":{"rendered":"How to Password Protect Directories with Nginx?"},"content":{"rendered":"<p>You can password protect a directory on the Apache server using .htaccess and .htpasswd files. But, .htaccess files are not supported on Nginx.<\/p>\n<p>Still, you can password protect your directories by using a basic_auth.conf file instead.<\/p>\n<h3>Creating the File<\/h3>\n<p>1. Log into your server using SSH.<\/p>\n<p>2. Go to your user&#8217;s directory.<\/p>\n<p>3. Check for the <strong>\/home\/username\/nginx\/example.com<\/strong> directory. This isn\u2019t available by default; you need to create it by running the below command:<\/p>\n<pre class=\"lang:default decode:true \">[server]$ mkdir -p nginx\/example.com<\/pre>\n<p>4. In this directory, add a file named <strong>&#8216;basic_auth.conf&#8217;<\/strong> with the below command:<\/p>\n<pre class=\"lang:default decode:true \">location \/ {\r\nauth_basic \"Restricted\";\r\nauth_basic_user_file \/home\/username\/nginx\/example.com\/.htpasswd;\r\n}<\/pre>\n<ul>\n<li>The auth_basic parameter is simply the title of the prompt seen by the user when he visits this directory.<\/li>\n<li>The auth_basic_user_file parameter showcases the location of the password file. Check how its path is set to the \/nginx directory.<\/li>\n<\/ul>\n<div style=\"background-color: #e0ffff; padding: 10px;\"><strong>Note:<\/strong> In this example, the <strong>&#8216;location&#8217;<\/strong> directive password protects the complete domain as it&#8217;s pointing to <strong>&#8216;\/&#8217;<\/strong>.<br \/>\nFor password protections a subdirectory, change the<strong> &#8216;location&#8217;<\/strong> directive as below:<\/p>\n<pre class=\"lang:default decode:true \">location \/subdirectory\/<\/pre>\n<\/div>\n<p>5. Execute the following to create the <strong>.htpasswd<\/strong> file:<\/p>\n<pre class=\"lang:default decode:true \">[server]$ htpasswd -c \/home\/username\/nginx\/example.com\/.htpasswd LOGIN<\/pre>\n<ul>\n<li><strong>LOGIN<\/strong> indicates the username for authenticating in the login prompt.<\/li>\n<\/ul>\n<p>6. After you type that command, enter a password and confirm it when you are asked for:<\/p>\n<pre class=\"lang:default decode:true \">New password:\r\nRe-type new password:\r\nAdding password for user LOGIN<\/pre>\n<p>7. You will need to reload the nginx config file.<\/p>\n<p>8. Load the directory your <strong>\/home\/username\/nginx\/example.com\/basic_auth.conf<\/strong> points to in your browser. *In the example above, this would be your domain&#8217;s root directory as the<strong> &#8216;location&#8217;<\/strong> directive points to \/.<\/p>\n<p>9. Enter a user\/password when asked to log in.<\/p>\n<ul>\n<li>In this example, <strong>LOGIN<\/strong> is your username and you have the password create in the above step.<\/li>\n<\/ul>\n<p>This way you can password protect your directing with Nginx.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can password protect a directory on the Apache server using .htaccess and .htpasswd files. But, .htaccess files are not supported on Nginx. Still, you can password protect your directories by using a basic_auth.conf file instead. Creating the File 1. Log into your server using SSH. 2. Go to your user&#8217;s directory. 3. Check for [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[771,770,772],"class_list":["post-7333","post","type-post","status-publish","format-standard","placeholder-for-hentry","category-howtos","tag-password-protect-directories","tag-password-protect-directories-using-nginx","tag-protect-directories-with-password"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Learn to Password Protect Directories With Nginx in Easy Steps<\/title>\n<meta name=\"description\" content=\"The article describes steps to password protect directories with Nginx by using the basic_auth.conf file.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Learn to Password Protect Directories With Nginx in Easy Steps\" \/>\n<meta property=\"og:description\" content=\"The article describes steps to password protect directories with Nginx by using the basic_auth.conf file.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Hosting FAQs by MilesWeb\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-07T11:29:14+00:00\" \/>\n<meta name=\"author\" content=\"Pallavi Godse\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Pallavi Godse\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/\"},\"author\":{\"name\":\"Pallavi Godse\",\"@id\":\"https:\/\/www.milesweb.in\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945\"},\"headline\":\"How to Password Protect Directories with Nginx?\",\"datePublished\":\"2019-06-07T11:29:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/\"},\"wordCount\":284,\"commentCount\":0,\"keywords\":[\"password protect directories\",\"password protect directories using Nginx\",\"protect directories with password\"],\"articleSection\":[\"How-Tos\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/\",\"url\":\"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/\",\"name\":\"Learn to Password Protect Directories With Nginx in Easy Steps\",\"isPartOf\":{\"@id\":\"https:\/\/www.milesweb.in\/hosting-faqs\/#website\"},\"datePublished\":\"2019-06-07T11:29:14+00:00\",\"author\":{\"@id\":\"https:\/\/www.milesweb.in\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945\"},\"description\":\"The article describes steps to password protect directories with Nginx by using the basic_auth.conf file.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.milesweb.in\/hosting-faqs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Password Protect Directories with Nginx?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.milesweb.in\/hosting-faqs\/#website\",\"url\":\"https:\/\/www.milesweb.in\/hosting-faqs\/\",\"name\":\"Web Hosting FAQs by MilesWeb\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.milesweb.in\/hosting-faqs\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.milesweb.in\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945\",\"name\":\"Pallavi Godse\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.milesweb.in\/hosting-faqs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3602d0ac1ab89332aea8205dcf474af4ff4eb319cfd584544038908cd5e4c966?s=96&d=blank&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3602d0ac1ab89332aea8205dcf474af4ff4eb319cfd584544038908cd5e4c966?s=96&d=blank&r=g\",\"caption\":\"Pallavi Godse\"},\"description\":\"Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.\",\"sameAs\":[\"https:\/\/www.milesweb.in\"],\"url\":\"https:\/\/www.milesweb.in\/hosting-faqs\/author\/pallavi\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Learn to Password Protect Directories With Nginx in Easy Steps","description":"The article describes steps to password protect directories with Nginx by using the basic_auth.conf file.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/","og_locale":"en_US","og_type":"article","og_title":"Learn to Password Protect Directories With Nginx in Easy Steps","og_description":"The article describes steps to password protect directories with Nginx by using the basic_auth.conf file.","og_url":"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/","og_site_name":"Web Hosting FAQs by MilesWeb","article_published_time":"2019-06-07T11:29:14+00:00","author":"Pallavi Godse","twitter_misc":{"Written by":"Pallavi Godse","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/#article","isPartOf":{"@id":"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/"},"author":{"name":"Pallavi Godse","@id":"https:\/\/www.milesweb.in\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945"},"headline":"How to Password Protect Directories with Nginx?","datePublished":"2019-06-07T11:29:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/"},"wordCount":284,"commentCount":0,"keywords":["password protect directories","password protect directories using Nginx","protect directories with password"],"articleSection":["How-Tos"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/","url":"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/","name":"Learn to Password Protect Directories With Nginx in Easy Steps","isPartOf":{"@id":"https:\/\/www.milesweb.in\/hosting-faqs\/#website"},"datePublished":"2019-06-07T11:29:14+00:00","author":{"@id":"https:\/\/www.milesweb.in\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945"},"description":"The article describes steps to password protect directories with Nginx by using the basic_auth.conf file.","breadcrumb":{"@id":"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.milesweb.in\/hosting-faqs\/password-protect-directories-with-nginx\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.milesweb.in\/hosting-faqs\/"},{"@type":"ListItem","position":2,"name":"How to Password Protect Directories with Nginx?"}]},{"@type":"WebSite","@id":"https:\/\/www.milesweb.in\/hosting-faqs\/#website","url":"https:\/\/www.milesweb.in\/hosting-faqs\/","name":"Web Hosting FAQs by MilesWeb","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.milesweb.in\/hosting-faqs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.milesweb.in\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945","name":"Pallavi Godse","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.milesweb.in\/hosting-faqs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3602d0ac1ab89332aea8205dcf474af4ff4eb319cfd584544038908cd5e4c966?s=96&d=blank&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3602d0ac1ab89332aea8205dcf474af4ff4eb319cfd584544038908cd5e4c966?s=96&d=blank&r=g","caption":"Pallavi Godse"},"description":"Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.","sameAs":["https:\/\/www.milesweb.in"],"url":"https:\/\/www.milesweb.in\/hosting-faqs\/author\/pallavi\/"}]}},"_links":{"self":[{"href":"https:\/\/www.milesweb.in\/hosting-faqs\/wp-json\/wp\/v2\/posts\/7333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.milesweb.in\/hosting-faqs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.milesweb.in\/hosting-faqs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.milesweb.in\/hosting-faqs\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.milesweb.in\/hosting-faqs\/wp-json\/wp\/v2\/comments?post=7333"}],"version-history":[{"count":1,"href":"https:\/\/www.milesweb.in\/hosting-faqs\/wp-json\/wp\/v2\/posts\/7333\/revisions"}],"predecessor-version":[{"id":7334,"href":"https:\/\/www.milesweb.in\/hosting-faqs\/wp-json\/wp\/v2\/posts\/7333\/revisions\/7334"}],"wp:attachment":[{"href":"https:\/\/www.milesweb.in\/hosting-faqs\/wp-json\/wp\/v2\/media?parent=7333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.milesweb.in\/hosting-faqs\/wp-json\/wp\/v2\/categories?post=7333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.milesweb.in\/hosting-faqs\/wp-json\/wp\/v2\/tags?post=7333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}