Bruno Miranda's Notebook

Personal Blog about Ruby on Rails, XHTML, CSS, and Design

Password protect a folder in nginx

Posted on October 29, 2007 at 06:01 PM

What you would normally do in apache with an .htaccess file, nginx requires a different approach.

In sites-enabled, edit your site’s config file by adding the following inside the server block

location = /secret-folder {
  auth_basic            "Restricted";
  auth_basic_user_file  /usr/nginx/htpasswd;
}

Make sure you create a htpasswd file in the correct location.

htpasswd -b htpasswd NewUser NewPassword

Very useful nginx wiki.

Good Luck!

Tags: nginx
Hierarchy: previous, next

Comments

There are 4 comments on this post. Post yours →


Thanks Bruno, short and sweet.

Denis

There is an error in your example. location = /secret-folder {…} will not protect the content of the folder, you need to use location ^~ /secret-folder {…}

See http://wiki.codemongers.com/NginxHttpCoreModule#location for the detailed explanation.


Great,

Thanks Bruno.


“short and sweet” and will not work for /secret-folder/secret-file.html because of “=” sign

try this:

location ~ ^/secret-folder { auth_basic “Restricted”; authbasicuser_file /etc/nginx/htpass; }

Post a comment

Required fields in bold.

 

Visit the Archives →