nginx points the sub-directory of an alias folder to the base directory

Starry asked:

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?

My answer:


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


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.