{"id":18631,"date":"2024-05-24T20:56:32","date_gmt":"2024-05-24T20:56:32","guid":{"rendered":"https:\/\/hostvento.com\/kb\/web-hosting\/getting-started-guide\/how-to-use-the-knowledge-base\/how-to-password-protect-directories-with-nginx\/"},"modified":"2024-06-17T08:55:23","modified_gmt":"2024-06-17T08:55:23","slug":"how-to-password-protect-directories-with-nginx","status":"publish","type":"docs","link":"https:\/\/www.hostvento.com\/kb\/docs\/hosting-faqs\/how-to-password-protect-directories-with-nginx\/","title":{"rendered":"How to Password Protect Directories with Nginx?"},"content":{"rendered":"\n<p>The.htaccess and.htpasswd files on the Apache server can be used to password-protect a directory. However, Nginx does not support.htaccess files.<\/p>\n\n\n\n<p>Still, you can password protect your directories by using a basic_auth.conf file instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating the File<\/h3>\n\n\n\n<p>1. Log into your server using SSH.<\/p>\n\n\n\n<p>2. Go to your user\u2019s directory.<\/p>\n\n\n\n<p>3. Check for the&nbsp;<strong>\/home\/username\/nginx\/example.com<\/strong>&nbsp;directory. This isn\u2019t available by default; you need to create it by running the below command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[server]$ mkdir -p nginx\/example.com<\/pre>\n\n\n\n<p>4. In this directory, add a file named&nbsp;<strong>\u2018basic_auth.conf\u2019<\/strong>&nbsp;with the below command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">location \/ {\nauth_basic \"Restricted\";\nauth_basic_user_file \/home\/username\/nginx\/example.com\/.htpasswd;\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The auth_basic parameter is simply the title of the prompt seen by the user when he visits this directory.<\/li>\n\n\n\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\n\n\n<p><strong>Note:<\/strong>&nbsp;In this example, the&nbsp;<strong>\u2018location\u2019<\/strong>&nbsp;directive password protects the complete domain as it\u2019s pointing to&nbsp;<strong>\u2018\/\u2019<\/strong>.<br>For password protections a subdirectory, change the<strong>&nbsp;\u2018location\u2019<\/strong>&nbsp;directive as below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">location \/subdirectory\/<\/pre>\n\n\n\n<p>5. Execute the following to create the&nbsp;<strong>.htpasswd<\/strong>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[server]$ htpasswd -c \/home\/username\/nginx\/example.com\/.htpasswd LOGIN<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>LOGIN<\/strong>\u00a0indicates the username for authenticating in the login prompt.<\/li>\n<\/ul>\n\n\n\n<p>6. After you type that command, enter a password and confirm it when you are asked for:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">New password:\nRe-type new password:\nAdding password for user LOGIN<\/pre>\n\n\n\n<p>7. You will need to reload the nginx config file.<\/p>\n\n\n\n<p>8. Load the directory your&nbsp;<strong>\/home\/username\/nginx\/example.com\/basic_auth.conf<\/strong>&nbsp;points to in your browser. *In the example above, this would be your domain\u2019s root directory as the<strong>&nbsp;\u2018location\u2019<\/strong>&nbsp;directive points to \/.<\/p>\n\n\n\n<p>9. Enter a user\/password when asked to log in.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In this example,\u00a0<strong>LOGIN<\/strong>\u00a0is your username and you have the password create in the above step.<\/li>\n<\/ul>\n\n\n\n<p>This way you can password protect your directing with Nginx.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The.htaccess and.htpasswd files on the Apache server can be used to password-protect a directory. However, Nginx does not support.htaccess files. 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\u2019s directory. 3. Check for the&nbsp;\/home\/username\/nginx\/example.com&nbsp;directory. This isn\u2019t [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":23377,"menu_order":102,"comment_status":"closed","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-18631","docs","type-docs","status-publish","hentry","no-post-thumbnail"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/docs\/18631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/comments?post=18631"}],"version-history":[{"count":2,"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/docs\/18631\/revisions"}],"predecessor-version":[{"id":18633,"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/docs\/18631\/revisions\/18633"}],"up":[{"embeddable":true,"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/docs\/23377"}],"wp:attachment":[{"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/media?parent=18631"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/doc_tag?post=18631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}