{"id":1723,"date":"2024-12-30T16:11:21","date_gmt":"2024-12-30T08:11:21","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=1723"},"modified":"2024-12-30T16:11:21","modified_gmt":"2024-12-30T08:11:21","slug":"how-to-set-access-control-for-the-static-files-that-served-by-nginx","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=1723","title":{"rendered":"How to set access control for the static files that served by nginx"},"content":{"rendered":"<p>To set an access password for static files served by Nginx, you can use HTTP Basic Authentication. Follow these steps:<\/p>\n<hr \/>\n<h3>1. <strong>Install <code>htpasswd<\/code> (if not already installed)<\/strong><\/h3>\n<p>The <code>htpasswd<\/code> utility is part of the Apache <code>apache2-utils<\/code> package on Debian-based systems or <code>httpd-tools<\/code> on RHEL-based systems.<\/p>\n<h4>Debian\/Ubuntu:<\/h4>\n<pre><code class=\"language-bash\">sudo apt update\nsudo apt install apache2-utils<\/code><\/pre>\n<h4>RHEL\/CentOS:<\/h4>\n<pre><code class=\"language-bash\">sudo yum install httpd-tools<\/code><\/pre>\n<hr \/>\n<h3>2. <strong>Create a Password File<\/strong><\/h3>\n<p>Use the <code>htpasswd<\/code> command to create a password file. Replace <code>username<\/code> with the desired username.<\/p>\n<pre><code class=\"language-bash\">sudo htpasswd -c \/etc\/nginx\/.htpasswd username<\/code><\/pre>\n<ul>\n<li>You\u2019ll be prompted to enter and confirm a password.<\/li>\n<li>Use the <code>-c<\/code> flag only the first time to create the file. Omit it when adding additional users.<\/li>\n<\/ul>\n<p>Example:<\/p>\n<pre><code class=\"language-bash\">sudo htpasswd \/etc\/nginx\/.htpasswd anotheruser<\/code><\/pre>\n<hr \/>\n<h3>3. <strong>Update Nginx Configuration<\/strong><\/h3>\n<p>Edit your Nginx configuration file to enable basic authentication for the location serving the static files.<\/p>\n<h4>Example:<\/h4>\n<p>If your static files are served from <code>\/var\/www\/static<\/code>:<\/p>\n<pre><code class=\"language-nginx\">server {\n    listen 80;\n    server_name example.com;\n\n    location \/static\/ {\n        root \/var\/www;\n        auth_basic &quot;Restricted Access&quot;;\n        auth_basic_user_file \/etc\/nginx\/.htpasswd;\n    }\n}<\/code><\/pre>\n<ul>\n<li><code>auth_basic<\/code>: Sets the realm name (visible in the authentication dialog box).<\/li>\n<li><code>auth_basic_user_file<\/code>: Specifies the path to the <code>.htpasswd<\/code> file.<\/li>\n<\/ul>\n<hr \/>\n<h3>4. <strong>Test Configuration<\/strong><\/h3>\n<p>Check the Nginx configuration for syntax errors:<\/p>\n<pre><code class=\"language-bash\">sudo nginx -t<\/code><\/pre>\n<p>If the test is successful, reload Nginx:<\/p>\n<pre><code class=\"language-bash\">sudo systemctl reload nginx<\/code><\/pre>\n<hr \/>\n<h3>5. <strong>Verify<\/strong><\/h3>\n<ul>\n<li>Open your browser and navigate to the URL serving the static files.<\/li>\n<li>You should be prompted for a username and password.<\/li>\n<\/ul>\n<hr \/>\n<h3>Additional Tips<\/h3>\n<ul>\n<li>Ensure the <code>.htpasswd<\/code> file is stored securely and only accessible by Nginx (e.g., permissions set to <code>600<\/code>).<\/li>\n<li>You can use HTTPS to encrypt the connection and enhance security.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>To set an access password for static files served by Nginx, you can use HTTP Basic Authentication. Follow these steps: 1. Install htpasswd (if not already installed) The htpasswd utility is part of the Apache apache2-utils package on Debian-based systems or httpd-tools on RHEL-based systems. Debian\/Ubuntu: sudo apt update sudo apt install apache2-utils RHEL\/CentOS: sudo [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=1723\" title=\"Permanent Link to: How to set access control for the static files that served by nginx\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1723","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1723"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1723"}],"version-history":[{"count":1,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1723\/revisions"}],"predecessor-version":[{"id":1724,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1723\/revisions\/1724"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}