I have a system in which users can enter their purchased domain into their profile, so when accessing their domain, it must replace its own domain, for example.
http://domain.com/custom-name to http://purchaseddomain.com .
Therefore, when they access their purchase domain, he must transfer them to his profile , including his navigation links, for example, the links on their page will be replaced by their acquired domain, for example, viewing their records will be:
http://domain.com/custom-name/records to http://purchaseddomain.com/records .
Tumblr allows this function, however I have no idea how this all works:

That is how I like to have such a function, I was looking for SO, but it didn't seem to help.
Now this is a problem, I'm not sure how I can verify, confirm and combine the purchased domain on my server without problems using PHP. For this I use Codeigniter.
Is there a robust, stable plugin / library or a detailed guide that might be able to include user domains masking the internal domain?
My server runs Ubuntu 11.10 on nginx 1.0.6.
The templates will be wonderful for me, what can I do - all I need help with is securely accept and merge my domain into my server.
EDIT: just looked at nginx VirtualHostExample , it looks good, but how can I dynamically add / remove this domain while the domain has an A record pointing to my server?