Symbolic link two domains/subdomains to same directories?

February 7, 2017 184 views
Nginx Ubuntu 16.04

Hello

I'm building a site to host on a droplet with a somewhat unconventional file structure and I'm not sure how to go about setting up the files.

Basically, I have two domains: domainA.com and domainB.com. On domainB, I also have many subdomains for different people. Each subdomain will have its own index, cusomtized with information for each person, but then all of the other pages are the same for each site.

So, domainA.com/work, domainB.com/work, and person.domainB.com/work will all pull from the same files.

I don't want a redirect because I want the domain name to stay the same (it will confuse people if it changes).

From research I think symlinks might do what I want, but I really have no clue, and can't find any documentation on doing something like this (I'm not super familiar with using the command line).

I also want to try and avoid just copying all of the directories to each subdomain for SEO and logistical purposes.

Any help would be much appreciated!!!

1 Answer

@stephenkistner

You can setup location blocks within each NGINX server block that point to the same directory.

For each of the domains and the sub-domain listed, you could use something such as the server block shown below. You'd simply change server_name to match the domain or sub-domain.

Essentially, you'll create a server block for each domain and sub-domain. So if you have 10 domains and 10 sub-domains, you'll end up with 20 server blocks, each customized for the domain or sub in question.

server {
    listen 80;
    server_name domainA.com www.domainA.com;

    root /path/to/web/root;

    location / {
        try_files $uri $uri/ =404;
    }

    location /work {
        root /path/to/work/directory;

        try_files $uri $uri/ =404;
    }
}

Breakdown

The first instance of root defines the web root for the domain. This should be the location where all the main files for the domain exist.

The first location block handles requests for each domain or sub-domain.

The second location block is where we handle serving ./work. We define a second root within this block as we need to specify where requests for ./work will pull from since you don't want to copy the files to each users directory.

With the above, all you'd really need to do is change server_name to match the domain or sub and then create one for each -- copy and paste.

Of course, the above assumes you only want to serve HTML files. If you want to serve PHP, we'd need to setup a PHP block as well. If you want to use NodeJS, Python or another language, we'd have to setup proxy_pass to handle proxying the requests to a port.

Have another answer? Share your knowledge.