11

I would like to add a condition in an adduser script to update nginx.conf for it to load ~/www as http://ipaddress/~user whenever I create a new user.

And when a user is named www.domainname it will host that domain name in the ~/www folder.

Is there a script that already does this?

1
  • No, there isn't. – mailq Sep 17 '11 at 12:53
27

You don't need to add anything to nginx upon user creation. Simply use something like this in your server block:

location ~ ^/~(.+?)(/.*)?$ {
    alias /home/$1/www$2;
    autoindex on;
}

Check your distributions /etc/skel

if you mkdir /etc/skel/www all userdirs created by adduser (or your distributions adduser-script) will have this directory by default.

13
  • now what if i want the users to override this once then have pointed their dns to that server? – Joseph Le Brech Oct 10 '11 at 11:42
  • I dont understand the question/issue :/ – Franz Bettag Oct 16 '11 at 10:22
  • if a user has a site hosted on ~username is there a way for the user to change it to domainname – Joseph Le Brech Oct 17 '11 at 7:26
  • 1
    You could give the user the possibility to have his own nginx.conf, something like (not 100% correct, but the direction is there): if (-e /home/$1/.nginx.conf) { include(/home/$1/.nginx.conf); } there the user could do his own setup, but also break the server-restart. this is a bit of a compilcated issue. – Franz Bettag Oct 17 '11 at 10:56
  • 1
    that actually rocks :) – Joseph Le Brech Oct 18 '11 at 13:52
1

I just stumbled upon this and the accepted answer looks VERY suspicious to me. As Nick ODell pointed out in the comments of the accepted answer, it is probably susceptible to directory traversal attacks.

A better solution is to have a main web root in the server block and then use a symlink from the web root to the user directory. You can have multiple symlinks point to the same target directory:

/var/www/user1 -> /home/user1/www
/var/www/domainname -> /home/user1/www

This type of approach also works better with try_files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.