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!
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.
×