nginx location redirect to folder with name as requested domain

Tomas Prado asked:

I’m new to nginx and I want to do this:

If the URL contains "/assets" redirect to "/communities/$url_domain"

In other words:

Requested: ""

It’s something super simple but it’s not working.

What I have done

location /assets {
    root /communities/$host;

But whenever I try to access a file from /assets I get a

404 Not Found


What I’m doing wrong?


I activated logs and saw this:

2016/02/10 17:27:16 [error] 31#0: *1 open() "/communities/" failed (2: No such file or directory), client:, server: _, request: "GET /assets/superfile.png HTTP/1.1", host: ""

I’ll investigate more If I can extract the domain part of the url

Directory structure


So, for example:

  1. Users will request this URL

  2. Users will see the same URL.

  3. Nginx internally will look for:

My answer:

You need to use an alias instead:

location /assets/ {
    alias /communities/$host/;

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.