Nginx. Wild card Alias location. Is it possible

nginx

Using the http://wiki.nginx.org/NginxHttpCoreModule#alias example regarding how to use sub directory aliases, I've noticed it does not mention how to do it dynamically.

In the following example…

location ~ ^/download/(.*)$ {
  alias /home/website/files/$1;
}

…the request "/download/book.pdf" will return the file "/home/website/files/book.pdf". Note again that only part of the request URI after the location is appended to the path defined by alias.

If I'm creating a folder labeled the username that the user created once signing up, then this folder name would always be unique and different. How do I alter the regex in the example above so that the word download also becomes a wild card sub directory?

location ~ ^/randomusername/(.*)$ {
  alias /home/website/profiles/randomusername/$1;
}

Best Answer

You need a little background about regex.

Please try the code below and see if it helps.

location ~ ^/([^/]+)/([^/?]+)$ {
  alias /home/website/profiles/$1/$2;
}
Related Topic