Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am new to Nginx. Now I have a confusion on nginx configurations:

My web site contains folders in different locations:

location / {
  root /Path1;
}

location ^~ /download {
  alias /Path2;
}

When I query http://mysite/download, I am accessing the content of /Path2 instead of /Path1

Now I want to add a sub-directory under download containing my private files, and I want to add password protection. So I add:

location /download/personal {
  auth_basic "Admin Login";
  auth_basic_user_file Path_to_htpasswd;
}

But I got 404 error when querying http://mysite/download/personal after key in my credentials. According to nginx error log, I am directed to /Path1/download/personal, which does not exist. How can I configure nginx, such that all access to http://mysite/download/* will be directed to the same directory in /Path2?

Update:

I may find a solution: just put the sub-directory configuration in the parent conf:

location ^~ /download {
  alias /Path2;
  location /download/personal {
    auth_basic "Admin Login";
    auth_basic_user_file Path_to_htpasswd;
  }
}

Is it correct? Or are there better solutions?

share|improve this question

1 Answer 1

You already know how to do this. You need to add an alias that points to the correct directory.

share|improve this answer
    
Yes. That is a solution. But then for every subfolder I created I have to make the changes. Is there any solution to configure http://mysite/personal/* all point to /Path2? –  Starry Jun 7 at 3:15
    
Why do you need to create so many locations? You should more clearly explain what you're trying to accomplish. –  Michael Hampton Jun 7 at 3:21
    
I have modified my post :) I just want to create a public accessible folder, in which there exists an encrypted subfolder. –  Starry Jun 7 at 3:36

Your Answer

 
discard

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.