Home  >  Q&A  >  body text

How to configure nginx to configure domain names for each subdirectory in a virtual host?

How to configure nginx to configure domain names for each subdirectory in a virtual host?
The requirement is that two directories, home and admin
, are placed in a virtual host. Home binding home.xx.com
admin binds admin.xx.com
I don’t want to create two virtual hosts.

高洛峰高洛峰2683 days ago551

reply all(1)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-16 17:30:28

    server {
    
        listen 80;
        server_name *.xx.com;
    
        # set a default root
        set $rootdir /home/xx/public_html;
    
        # try if is a subdomain
        if ($host ~* ^(.+)\.xx\.com$) {
            set $rootdir /home/xx/subdomains/$1;
        }
    
        root $rootdir;
    }
    

    reply
    0
  • Cancelreply