By mkel23
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!
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!
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.
Heya,
To configure subdomains with different root folders in Nginx, you need to create separate server blocks for each subdomain in your Nginx configuration file. Each server block will specify the subdomain and its corresponding root folder:
Create Subdomain Configuration Files:
First, create separate configuration files for each subdomain in the /etc/nginx/sites-available/ directory. Name these files according to your subdomains. For example, you can create a configuration file for my.domain and another for sub.my.domain:
sudo nano /etc/nginx/sites-available/my.domain
Add the configuration for my.domain:
server {
listen 80;
server_name my.domain www.my.domain;
root /path/to/main/root/folder;
# ... Other settings for my.domain ...
}
Now, create a configuration file for sub.my.domain:
sudo nano /etc/nginx/sites-available/sub.my.domain
Add the configuration for sub.my.domain:
server {
listen 80;
server_name sub.my.domain;
root /path/to/subdomain/root/folder;
# ... Other settings for sub.my.domain ...
}
Make sure to replace /path/to/main/root/folder and /path/to/subdomain/root/folder with the actual paths to your root folders.
Enable the Configuration Files:
Create symbolic links to enable the configuration files in the /etc/nginx/sites-enabled/ directory:
sudo ln -s /etc/nginx/sites-available/my.domain /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/sub.my.domain /etc/nginx/sites-enabled/
DNS Configuration:
Ensure that the DNS records for my.domain, www.my.domain, and sub.my.domain are properly configured to point to the IP address of your server.
Default Server Block:
Make sure you have a default server block defined in your Nginx configuration. This block will handle requests that don’t match any of the specified server_name values. You can create a default server block by adding the following configuration:
server {
listen 80 default_server;
server_name _;
root /path/to/default/root/folder;
# ... Other settings for the default server ...
}
Replace /path/to/default/root/folder with the path to the root folder for requests that don’t match any subdomains.
By following these steps, requests to my.domain, www.my.domain, and sub.my.domain should be routed to their respective root folders, with the default server handling any other requests. Make sure that your DNS records are set up correctly to direct traffic to the appropriate subdomains.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.