Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried so many methods and get stick hours with this. I edit /etc/nginx/nginx.conf and write these lines.

location /home/user/domains/ {
  auth_basic "Restricted";
  auth_basic_user_file /home/user/domains/;

and I use crypt(3) encryption to make password with the command mkpasswd. Then I did with the given procedure user:encryptedpasswd in .htpasswd.

But things does not work as said. Let me know if anyone know how I can exactly make configure for my purpose! I also reload my nginx with /etc/init.d/nginx reload

share|improve this question
up vote 2 down vote accepted

The location /home/user/domains/ line is wrong, as it uses file system path, while location works with URI. Correct config will look like

location /lockfolder/ {
    auth_basic "Restricted";
    auth_basic_user_file /path/to/htpassd;

It is expected to restrict requests to

Another possible pitfall is other locations, notably more specific ones and/or given by regular expressions. You have to make sure location you wrote actually matches requests you want it to match. See for the documentation on location matching details.

share|improve this answer
ya now worked with by editing /etc/nginx/site-enabled/mysite I am going wrong because I am editing the main nginx config file.There is no problem with htpasswd file,now all the things working fine :). – Tin Aung Linn Nov 4 '12 at 13:33
@TinAungLinn If your question is resolved, remember to accept the answer that resolved your problem by clicking the outline of the check mark next to it. This lets others know the question is resolved, and you also gain reputation. Welcome to Server Fault! – Michael Hampton Nov 4 '12 at 13:37

Try this:

location /lockfolder {
  auth_basic            "Restricted";
  auth_basic_user_file  htpasswd;

Since version 0.6.7 the auth_basic_user_file path is relative to directory of nginx configuration file nginx.conf. If Apache is installed, you can create the password file using the htpasswd program included.

HttpAuthBasicModule Docs

share|improve this answer
Cant't get the meaning of it I did that manual too. – Tin Aung Linn Nov 4 '12 at 12:30
Do not use full path to the directory, but rather the url relative to the server root (the url that you can see in the browser). Make sure that you don't have any other rule that matches this location. – FINESEC Nov 4 '12 at 13:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.