Question

How to setup subdomains for different root folders with nginx

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!

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.