Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

I am trying to setup user subdomains, serving content from specific folders: served from (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 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?

share|improve this question
up vote 3 down vote accepted

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/ instead of /var/www/username/.

share|improve this answer
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 '11 at 1:59

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;
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.