0

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/example.com/public_html/lockfolder/ {
  auth_basic "Restricted";
  auth_basic_user_file /home/user/domains/example.com/.htpasswd;
}

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

growse
  • 8,020
  • 13
  • 74
  • 115

2 Answers2

2

The location /home/user/domains/example.com/public_html/lockfolder/ 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 http://www.example.com/lockfolder/something.

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 http://nginx.org/r/location for the documentation on location matching details.

Maxim Dounin
  • 3,596
  • 19
  • 23
  • 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 04 '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 04 '12 at 13:37
0

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

techraf
  • 4,243
  • 8
  • 29
  • 44
FINESEC
  • 1,371
  • 7
  • 8
  • Cant't get the meaning of it I did that manual too. – Tin Aung Linn Nov 04 '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 04 '12 at 13:30