Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My setup is:

  • Nginx(80) as a transparent proxy
  • Apache(81) as back-end web server

The paths to each domain on the server are like this:


Of course to access the files you need to use the domain name of each website. But I want a way to access all the domains using an URL like this: 

How can I do this? This URL will be used to access ONLY static files, so I think it will be best to use Nginx for this.

share|improve this question
How does $username map to $domainname? – mgorven Aug 12 '12 at 5:46
$username is just the username of the user and it can be anything. A user can have many domains and $domainname is a folder named – Jim Aug 12 '12 at 11:20
up vote 1 down vote accepted

The problem is determining the username for a specific domain. Actually getting nginx to search through all the usernames to find this is tricky and not optimal. There are two approaches I can think of.

Symlink all domains into one directory

Create a directory with symlinks to every domain, for example:

/srv/www/all/ -> /srv/www/alice/data/www/
/srv/www/all/ -> /srv/www/alice/data/www/
/srv/www/all/ -> /srv/www/bob/data/www/

And then just setup a virtual host in nginx with /srv/www/all as the root.

Manually configure the mapping in nginx

The map module is probably the simplest to maintain.

map $uri $username {
    /    alice;
    /    alice;
    /    bob;

root /srv/www;
rewrite ^/([^/]+)/(.*)$ /$username/data/www/$1/$2 last;
share|improve this answer

Your Answer


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.