16535c0a5db3a1ca9bd218eccd13b9e1d9f70b9c
By:
mkel23

How to setup subdomains for different root folders with nginx

March 27, 2015 4.9k views
Nginx DigitalOcean DNS Ubuntu

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!

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.

      • @mkel23 Thanks a lot. I finally managed to get it working. Do you have any advice deploying? Do you use any automation system for Flask or git or SFTP?

Have another answer? Share your knowledge.