Configure complex dynamic urls

Hi I’ve been trying to configure an nginx to accept a complex dynamic
url,
so far I got this. (http://pastebin.com/aSmW5GPn), this what I got so
far.

Here is the deal, the url should be working like these:

mydomain.com - will be the normal domain with information, user
registration
and user authentication. (http://mydomain.com/signin,
http://mydomain.com/register)

the user can create database from their account.
mydomain.com/database/create, view the database information
mydomain.com/database/view, and list all the database htey have created
http://mydomain.com/database/list

so far everything can be done and I can manage this.

The issue start here. to the user manage their database, create tables,
etc,
I want the url format to be: http://{user}.mydomain.com/{database}, this
should proxy to a system/program that handles the database management
(that
should be hidden and only accessed by the url mention before).

If user go to http://{user}.mydomain.com/{database}/login, they are
actually
going to the another database manager, pre-filling the username and
database
to be used.

Thanks for you help in advance

PS. mydomain.com is using nginx+php and the database management software
is
using htaccess+php+nginx

Posted at Nginx Forum: