Question

Multiple Domains: subdomain.site2.com redirects to site1.com (default server)

Posted January 22, 2020 439 views
NginxDNSLEMP

Hi,

I have two domains setup on the same droplet, with two separate URLs. I’m now trying to setup a third site, as a subdomain of one of these. However, it keeps redirecting to the other domain.

So, if you type in x.site2.com it goes straight to site1.com - when I just want it to go to start accessing the directory for the new site / subdomain.

I’ve done all the usual guff of setting a site up, creating the directory, files, config file, syslink, restart nginx and I’ve setup A name and C name records. A record points x.site2.com to the IP address for the droplet, and CNAME points www.x.site2.com to x.site2.com.

Below are the config files (using “site1”, “site2”, “x.site2” as substitutes here - obviously these aren’t the actual names)

I’ve tried a bunch of things, including just listing the subdomain in the config file for site2 and have it just redirect from the subdomain to site2’s domain, and still no love.
I’ve also tried deleting the config file for Site1, to see if it’ll stop redirecting there. When I do that, it still redirects there, it just gives me a 404 error, since it has nothing setup.
I’ve also tried deleting the config file for Site1 and then setting the subdomain as default_server, and this sends me to site2.
So I suspect the issue is somewhere to do with my CNAME / A Records and not the configs, but here they are anyway:

Site1 (default server):

    upstream php {
            server unix:/tmp/php-cgi.socket;
            server 127.0.0.1:9000;
    }

    server {
    listen 80 default_server;
    listen [::]:80 default_server;
            server_name www.site1.com site1.com;
            root /srv/www/site1;
            index index.html index.php index.htm index.nginx-debian.html;

            location = /favicon.ico {
                    log_not_found off;
                    access_log off;
            }

            location = /robots.txt {
                    allow all;
                    log_not_found off;
                    access_log off;
            }

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

            location ~ \.php$ {

                         include snippets/fastcgi-php.conf;
                         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                    fastcgi_intercept_errors on;
            }

            location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                    expires max;
                    log_not_found off;
            }

                 location ~ /\.ht {
                     deny all;
                 }
    }

Site2:

    server {
        listen 80;
        listen [::]:80;

        root /srv/www/site2/public_html;
        index index.html index.php index.htm index.nginx-debian.html;

        server_name site2.com www.site2.com;

        location / {
            try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }


        location ~ /\.ht {
            deny all;
        }
    }

x.site2 (subdomain):

    server {
    listen 80;
    listen [::]:80;
            server_name x.site2.com;
            root /srv/www/x-site2;
            index index.html index.php index.htm index.nginx-debian.html;

            location = /favicon.ico {
                    log_not_found off;
                    access_log off;
            }

            location = /robots.txt {
                    allow all;
                    log_not_found off;
                    access_log off;
            }

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

            location ~ \.php$ {
                         include snippets/fastcgi-php.conf;
                         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                    fastcgi_intercept_errors on;
            }

            location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                    expires max;
                    log_not_found off;
            }

                 location ~ /\.ht {
                         deny all;
                 }
    }
edited by MattIPv4

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.

×
1 answer
Show answer This answer has been marked as resolved by rorybot.
Submit an Answer