Question

How to setup subdomains for different root folders with nginx

Posted March 27, 2015 13.8k views
Nginx Ubuntu DigitalOcean DNS

Hello,

I am trying to figure out how to setup a subdomain for my droplet that uses a different root folder for files.

I want my.domain and all variants (www.my.domain *.my.domain) to go to my main root folder and one specific subdomain (sub.my.domain) to go to a different root folder.

However, what is happening is that when I navigate to sub.my.domain, I am taken to the website located within the files of my main root folder instead of the site files located in the sub domain’s root folder (essentially, the subdomain doesn’t exist and is captured by *.my.domain).

Here is what I know:

Zone File:

$ORIGIN my.domain.
$TTL 1800
my.domain. IN SOA ns1.digitalocean.com. hostmaster.my.domain. 1427472545 10800 3600 604800 1800
my.domain. 1800 IN NS ns1.digitalocean.com.
my.domain. 1800 IN NS ns2.digitalocean.com.
my.domain. 1800 IN NS ns3.digitalocean.com.
my.domain. 1800 IN A drop.let.ip.address
*.my.domain. 1800 IN CNAME my.domain.
sub.my.domain. 1800 IN CNAME my.domain.

My default nginx file in sites-available linked to sites-enabled:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/domain/public;
        index index.php index.html index.htm;

        server_name *.my.domain;

        location / {
                try_files $uri $uri/ /index.php?query_string;
        }

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

My sub nginx file in sites-available linked to sites-enabled:

server {
        listen 80;
        listen [::]:80 ipv6only=on;

        root /var/www/sub/public;
        index index.php index.html index.htm;

        server_name sub.my.domain;

        location / {
                try_files $uri $uri/ /index.php?query_string;
        }

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

And the lines I added to my hosts file:

drop.let.ip.address my.domain
drop.let.ip.address sub.my.domain

Any help is much appreciated!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

2 answers

I think the error is in the record . You put CNAME and actually have to be Record A. I believe that with this change already solve the problem.

Found the issue: nginx goes through the config files in order. So, I placed both in the same config file, with the specific subdomain on the top. This fixed the issue.

  • @mkel23 Can you share your solution in more details? I’m trying to accomplish the same thing, and can’t make it work with what you’ve mentioned. Thanks!

    • @pavloko In your sites-enabled directory, under your default configuration, make sure you put the subdomain before the catch all (I had *.my.domain) so that the server will catch the sub.my.domain before it gets to *.my.domain. I think it also checks the configuration files in alphabetical order, so if you’d prefer to have to separate files, then make sure you name it something that comes before default, but I have not tested that; I only put them in one file and that works.

Submit an Answer