3

I am trying to setup user subdomains, serving content from specific folders: www.example.com/username served from username.example.com (just like github pages).

I've looked at Nginx rewrites, but I don't want the browser to redirect--I want the domain to be username.example.com. Anyway, a comment on this question says that I cannot rewrite host, only proxy to it. I tried to setup a proxy_pass, but all of the documentation and examples show it being used to (obviously) proxy to a service on another host or port, but in my case I want to just proxy to another location on the same host and port.

Is this the appropriate way to tackle this problem, and if so, what is the right Nginx config syntax?

2 Answers 2

4

The simple way to handle this is to have nginx treat each of the directories as a different virtual host.

server_name _;
root /var/www/$host;

This would require the physical path to the files to be /var/www/username.example.com/ instead of /var/www/username/.

1
  • This works perfectly; thank you. Also, I am using location ~ (/stylesheets|/javascripts) to set the root for files shared by all users.
    – Kevin L.
    Mar 13, 2011 at 1:59
0

For reference, this is what I ended up putting in my nginx site config. Note that I'm matching the subdomain and setting the root from that, rather than from $host as in Shane's answer, so I can use /var/www/username on disk. Also, all of the users share stylesheets and javascripts folders.

server_name _;

set $myroot "";
if ($host ~ (.*)\.example\.com) {
          set $myroot $1;
}
if ($host ~ ^www\.example\.com) {
  set $myroot "";
}

location / {
  root   /var/www/$myroot;
  index  index.html index.htm;
  autoindex on;
}

location ~ ^(/stylesheets|/javascripts) {
  root /var/www;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .