{"id":11894,"date":"2014-02-25T14:56:47","date_gmt":"2014-02-25T09:26:47","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11894"},"modified":"2014-02-25T14:56:47","modified_gmt":"2014-02-25T09:26:47","slug":"http-authentication-using-nginx","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/http-authentication-using-nginx\/","title":{"rendered":"HTTP Authentication using Nginx"},"content":{"rendered":"<p>I got a requirement from one of my clients to setup a staging server that has a HTTP authentication, behind an ELB. but because of authentication it fails in the ELB health check. I did the following steps to configure it with HTTP authentication.<\/p>\n<ul>\n<li>Create a single PHP or HTML file and disable HTTP authentication for this file.<\/li>\n<li>In the configure health check section pass that file name in the Ping Path.<\/li>\n<li>ELB gets the response on configured Ping Path because authentication is disabled for this file.<\/li>\n<\/ul>\n<p>Few more tricks that can be used to setup HTTP authentication using nginx.<\/p>\n<p><strong>Create a htpasswd file with username : myuser &amp; password :\u00a0mypassword<\/strong><\/p>\n<p>Create a htpasswd that contains username and encrypted password. To create that file we need to install php CLI tools or it can be created from some other tools too.<\/p>\n<p>[shell]sudo apt-get install php5-cli<br \/>\nphp -a<br \/>\nphp &gt; echo crypt(&#8216;mypassword&#8217;, base64_encode(&#8216;mypassword&#8217;));<br \/>\nbX6j7x3Ep6RnU<br \/>\necho &#8216;myuser:bX6j7x3Ep6RnU&#8217; &gt;&gt; \/etc\/nginx\/htpasswd[\/shell]<\/p>\n<p><strong>Basic password protection<\/strong><\/p>\n<p>Add below code into the Nginx site configuration file that will enable authentication on the complete site.<\/p>\n<p>[shell]location \/ {<br \/>\n                  auth_basic  &quot;Restricted&quot;;<br \/>\n                  auth_basic_user_file  \/etc\/nginx\/htpasswd;<br \/>\n}[\/shell]<\/p>\n<p><span style=\"font-family: 'Courier 10 Pitch', Courier, monospace;font-size: 13px;font-style: normal;line-height: 1.5\">\u00a0<\/span><\/p>\n<p><strong>Open-access for a single IP, password-protect for everyone else<\/strong><\/p>\n<p>This will allow you to disable password for a single IP and enable password for the others.\u00a0This method is great during project development when you want to give access for a single IP.<\/p>\n<div>[shell]location \/ \u00a0{<br \/>\n                   satisfy any<br \/>\n                   allow \u00a0*.*.*.* ;<br \/>\n                   deny all;<br \/>\n                   auth_basic &quot;Restricted&quot;;<br \/>\n                   auth_basic_user_file \/etc\/nginx\/htpasswd;<br \/>\n}[\/shell]<\/p>\n<\/div>\n<p><strong>Open access for multiple IPs, password-protect for everyone else<\/strong><\/p>\n<p>That will allow you to disable password protection for multiple IPs.<\/p>\n<div>[shell]<br \/>\nlocation \/ \u00a0{<br \/>\n            satisfy any;<br \/>\n            allow \u00a0*.*.*.* ;<br \/>\n            allow \u00a0*.*.*.* ;<br \/>\n            deny all;<br \/>\n            auth_basic &quot;Restricted&quot;;<br \/>\n            auth_basic_user_file \/etc\/nginx\/htpasswd;<br \/>\n}[\/shell]<\/p>\n<\/div>\n<div>\n<p><strong>Password protection for everything except a single file<\/strong><\/p>\n<p>In a case you want to disable password protection for a single file only. I have used this technique countless times.<\/p>\n<div>[shell]<br \/>\nlocation \/ \u00a0{<br \/>\n            auth_basic &quot;Restricted&quot;;<br \/>\n            auth_basic_user_file \/etc\/nginx\/htpasswd;<br \/>\n            location \u00a0 \u00a0\/sample\/abc.html {<br \/>\n                                         auth_basic off;<br \/>\n            }<br \/>\n}[\/shell]<\/p>\n<p><strong>Password protect a single file<\/strong><\/p>\n<p>This will allow access to a single file while password-protecting everything else<\/p>\n<div>[shell]<br \/>\nlocation \/ \u00a0{<br \/>\n      \u00a0     location \/sample\/abc.html \u00a0 {<br \/>\n                                        auth_basic &quot;Restricted&quot;;<br \/>\n                                        auth_basic_user_file \/etc\/nginx\/htpasswd;<br \/>\n            }<br \/>\n}[\/shell]<\/p>\n<p><strong>Password protect a folder<\/strong><\/p>\n<p>If you have a use case to protect multiple files in a folder, So instead of protect multiple files you can protect that complete folder directly.<\/p>\n<p>[shell]<br \/>\nlocation \/ \u00a0{<br \/>\n            location \/sample\/ \u00a0{<br \/>\n                  \u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 auth_basic &quot;Restricted&quot;;<br \/>\n                               auth_basic_user_file \/etc\/nginx\/htpasswd;<br \/>\n            }<br \/>\n}[\/shell]<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I got a requirement from one of my clients to setup a staging server that has a HTTP authentication, behind an ELB. but because of authentication it fails in the ELB health check. I did the following steps to configure it with HTTP authentication. Create a single PHP or HTML file and disable HTTP authentication [&hellip;]<\/p>\n","protected":false},"author":678,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1],"tags":[1337,260,1336],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11894"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/678"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11894"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11894\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}