Unable to get subdomains working

September 28, 2016 1k views
DigitalOcean Nginx Ubuntu

I'm sure this is some stupid little thing I'm missing, so I'm reluctant to post, but I don't want to waste any more of my evening on something I can't solve.

On my server I want to setup a staging url which will host beta features before we push them live. However, despite putting in an A record and setting up my server configurations in nginx they don't want to work, I can only assume it's a small thing.

I have 2 nginx server files, both are enabled.

First is custom, this is the production url with HTTPS

server {
        listen 443 ssl;
        server_name allagandata.com www.allagandata.com;

        ssl_certificate /var/www/acme/.acmephp/master/certs/allagandata.com/fullchain.pem;
        ssl_certificate_key /var/www/acme/.acmephp/master/private/allagandata.com/private.pem;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_dhparam /var/www/acme/.acmephp/master/certs/allagandata.com/dhparam.pem;
        ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-$
        ssl_session_timeout 1d;
        ssl_session_cache shared:SSL:50m;
        ssl_stapling on;
        ssl_stapling_verify on;
        add_header Strict-Transport-Security max-age=15768000;

        root /var/www/Production/public;
        index index.php index.html index.htm;

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

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_intercept_errors on;
    }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
}

The second is the staging url:

server {
        listen 80;
        server_name staging.allagandata.com;

        root /var/www/AnExiledGod/public;
        index index.php index.html index.htm;

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

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_intercept_errors on;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
}

My DNS records:

$ORIGIN allagandata.com.
$TTL 1800
allagandata.com. IN SOA ns1.digitalocean.com. hostmaster.allagandata.com. 1475025426 10800 3600 604800 1800
allagandata.com. 1800 IN NS ns1.digitalocean.com.
allagandata.com. 1800 IN NS ns2.digitalocean.com.
allagandata.com. 1800 IN NS ns3.digitalocean.com.
staging.allagandata.com. 1800 IN A 162.243.102.77
allagandata.com. 1800 IN A 162.243.102.77

I did have a CNAME record in there but it didn't seem to do anything.

1 Answer
xMudrii September 28, 2016
Accepted Answer

Hi.

Make sure you enabled your server block.
You should have symlink from sites-available to sites-enabled.

  • sudo ln -s /etc/nginx/sites-available/staging /etc/nginx/sites-enables

Make sure you use corresponding file name instead of staging.
When you done it

  • sudo nginx -t

to verify configuration files.

If tests are OK, restart nginx and try again.

  • sudo systemctl restart nginx

Learn more about Server blocks.

About DNS records. I think you can also use A record as you do now. Personally I use CNAME - subdomain @ and it works flawless

When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server. In this guide, we'll discuss how to configure server blocks in Nginx on an Ubuntu...
  • I've done all of that, and I've rechecked about 3 times because I see this kind of message a lot around here.

    Both sites are enabled, configs are fine, and I've restarted nginx.

    I've changed the record from an A record to CNAME
    staging.allagandata.com. 1800 IN CNAME staging.allagandata.com.

    In fact. staging.allagandata.com is still loading despite me removing all but custom from the sites-enabled. Custom is the one shown above using SSL. But for server_name I'm not using wildcards.

    • I don't know what is current status of this but:

      ➜  ~ ping staging.allagandata.com
      ping: unknown host staging.allagandata.com
      

      Anyways I'm not sure this is correct CNAME record:

      staging.allagandata.com. 1800 IN CNAME staging.allagandata.com.
      

      Look at mine www just for example:

      subdomain.xmudrii.com. 1800 IN CNAME xmudrii.com.
      

      Look at parts I highlighted.
      This could mean two things:
      You added domain staging.allagandata.com. You should add CNAME record to allagandata.com domain.
      Or you put wrong value in second field.
      First field should have staging and second should allagandata.com..

      Keep in mind that DNS records needs time to be updated. It still can shown old content.
      Sometimes I also need to reboot Internet modem to fetch DNS changes (sounds stupid maybe, but it works) :)

      • Still happening the same. But I updated my CNAME record anyway. I'm using google's DNS servers which update VERY quickly, especially since I have a google domain and I'm flushing my DNS after every change.

        The site loads fine, the issue is it's not displaying the right stuff. Even though I removed the server block with staging.allagandata.com as the server name, the subdomain still loads, and even when I had that server block it was loading data from the wrong folder.

        • Hmmm... I'm getting out of ideas but while I'm thinking about it,
          did you tried default_server directive?
          How you can make it look:
          Change listen 443 ssl; to listen 443 ssl default_server;.
          Change listen 80; to listen 80 default_server;
          It will set allagandata.com server block default for ssl traffic, and staging.allagandata.com for staging.allagandata.com http traffic.

          You can try changing browser (or using incognito) or clearing browser cache, just for case

          If I get more ideas, I will post it here, but for now, I can't :P

Have another answer? Share your knowledge.