SubDomains: Cname or A Records

I'd like to add a couple of subdomains to my install.

I'm looking for one to be dev.example.com.

If I add dev.example.com under A records under DNS Manager, and add my server's IP address, people visiting example.com are directed to dev.example.com which is NOT what I want.

I would like dev.example.com to be a separate entity that resolves to the server's IP address but serves another folder. I've heard that this is done with CName's but I'm not too clear on that.

Would I just put in

Hostname: dev.example.com

Aliases to: dev.example.com

??

6 Replies

From a DNS standpoint, the end result is the same: dev.example.com returns your server's IP address. Doesn't matter how it's defined in the DNS. The response is entirely a function of your web server's configuration.

If I add dev.example.com under A records under DNS Manager, and add my server's IP address, people visiting example.com are directed to dev.example.com which is NOT what I want.

That's not how it works. (Re)directing users to one site or another is the job of your server software, not DNS. Nobody is getting (re)directed anywhere unless you explicitly configure your server to do so. CNAME has nothing to do with it. What you're looking for is VirtualHost.

If you're not sure, just use an A record for each domain and subdomain you want to use. That's the simplest and most intuitive way to connect a domain or subdomain with any given server. You can try experimenting with CNAME and other record types once you're comfortable working with DNS.

Thanks for the help everyone. I've been pretty much stumped for the past day. I've created an A record for analytics.mydomain.com with the IP address of my server. The IP address though resolves into analytics.mydomain.com instead of mydomain.com.

Here's the pertinent server block file for analytics.mydomain.com. Do you guys see any issues with it?

server {

listen 80;

server_name analytics.mydomain.com;

return 301 https://analytics.*mydomain*.com$request_uri;

}

server {

listen 443 ssl;

server_name analytics.mydomain.com;

root /home/forge/analytics.mydomain.com/public;

FORGE SSL (DO NOT REMOVE!)

ssl_certificate /etc/nginx/ssl/analytics.mydomain.com/8214/server.crt;

sslcertificatekey /etc/nginx/ssl/analytics.mydomain.com/8214/server.k$

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

index index.html index.htm index.php;

charset utf-8;

location / {

tryfiles $uri $uri/ /index.php?$querystring;

}

location = /favicon.ico { accesslog off; lognot_found off; }

location = /robots.txt { accesslog off; lognot_found off; }

access_log off;

error_log /var/log/nginx/analytics.mydomain.com-error.log error;

error_page 404 /index.php;

location ~ .php$ {

fastcgisplitpath_info ^(.+.php)(/.+)$;

fastcgi_pass unix:/var/run/php5-fpm.sock;

fastcgi_index index.php;

include fastcgi_params;

}

location ~ /.ht {

deny all;

}

}

The IP address though resolves into analytics.mydomain.com instead of mydomain.com.

What do you mean? Domains and subdomains resolve into IP addresses, not the other way around. (Technically, you can make IPs resolve back into domains if you set up "reverse DNS", but that's irrelevant for now.)

If you mean that you get analytics.yourdomain.com when you type your IP address into your browser's address bar, then yes, that's as it should be. When you connect to a server without using a proper hostname, nginx will simply respond with the first "server" block. If analytics.yourdomain.com is the first "server" block, nginx will respond with it. If yourdomain.com is the first "server" block, nginx will respond with it.

People usually set up a dummy "server" block that points to an empty directory and put it at the very top, so that anyone who tries to connect without using a proper hostname will just get a blank page or "404 Not Found" error. This makes it mandatory for everyone to use a proper hostname, such as a domain or subdomain that you explicitly set up.

Thank you for your help.

Yes, my issue is that my server's IP resolves to a subdomain; analytics.mydomain.com. I don't want this. I would rather have the IP address resolve to mydomain.com.

Would it be possible to set up a dummy server block in that case and have it redirect to the domain name of my choice.

I've tried doing this by creating a file called "a" which theoretically Nginx should run before anything else and put in the following code but it doesn't redirect:

server {

listen 80;

server_name mydomain.com;

return 301 https://www.*mydomain*.com$request_uri;

}

The IP -> name mapping is called a PTR record in DNS. This is defined in the "Remote Access" tab of the control panel where you'll see a link for "Reverse DNS".

(This mapping has NO IMPACT on what pages your web server sends out, though).

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct