Is it possible to have multiple subdomain levels with DNS and Nginx?

This seems like a simple question, but after some Google searching I can't seem to find the answer.

I have a staging server where I would like to mimic the production server as much as possible. Let's say I own example.com and I decide that I want to host a blog using blog.example.com. I would like to be able to stage the blog before I push it to production using the staging address blog.stage.example.com. I rather do this than buy another domain such as stage-example.com or use naming conventions like blog-stage.example.com.

Obviously, I would want blog.stage.example.com to point to the server running on the staging environment (e.g. IP 1.1.1.1), and blog.example.com to point to the server running on the production environment (e.g. IP 2.2.2.2).

I am using Nginx for my web back-end and have Gandi as a hosting provider where I manage my DNS zone file.

Is it possible to configure my domain names with the multi-level subdomains? Or, am I stuck with naming conventions like, blog-stage.example.com? Or, perhaps there is a better way to manage this that I don't know about?

If it is possible, what would the DNS zone file look like?

And finally, if it is possible, what would the Nginx configuration file look like?


Solution 1:

Your nginx virt hosts will look something like this:

server {
  listen 1.1.1.1:80;
  server_name blog.stage.example.com;

[...]

}

server {
  listen 2.2.2.2:80;
  server_name blog.example.com;

[...]

}

This assumes that you can assign two IP addresses to your web server. With the above configuration, one virtual server will bind to 1.1.1.1:80 and respond to blog.stage.example.com, and the other server will bind to 2.2.2.2:80 and respond to blog.example.com. Setting up the DNS will depend on how your DNS is managed, but it should be do-able.

Solution 2:

Sub-domains and hosting are a bit of an undefined, and you're best served asking them if they support something like that. It is quite possible to define an A record with multiple labels in it. For instance, you could put this in your DNS zone for example.com:

blog.stage             1.1.1.2
      blog             1.1.1.1

And it might work. Bind DNS does support that kind of thing, but whether or not your hosting provider lets you do that is another story entirely.

A better plan is to use something besides blog.

betablog              1.1.1.2
    blog              1.1.1.1

Since that's much more likely to be supported by your provider.

I'm not famililar enough with Nginx to provide multiple virtual-host examples for that.

Solution 3:

The correct answer comes from @sysadmin1138 and @cjc:

Nginx Configuration

Your nginx virt hosts will look something like this:

server {
  listen 1.1.1.1:80;
  server_name blog.stage.example.com;

[...]

}

server {
  listen 2.2.2.2:80;
  server_name blog.example.com;

[...]

}

This assumes that you can assign two IP addresses to your web server. With the above configuration, one virtual server will bind to 1.1.1.1:80 and respond to blog.stage.example.com, and the other server will bind to 2.2.2.2:80 and respond to blog.example.com. Setting up the DNS will depend on how your DNS is managed, but it should be do-able.

DNS

Sub-domains and hosting are a bit of an undefined, and you're best served asking them if they support something like that. It is quite possible to define an A record with multiple labels in it. For instance, you could put this in your DNS zone for example.com:

blog.stage             1.1.1.2
      blog             1.1.1.1

And it might work. Bind DNS does support that kind of thing, but whether or not your hosting provider lets you do that is another story entirely.

A better plan is to use something besides blog.

betablog              1.1.1.2
    blog              1.1.1.1

Since that's much more likely to be supported by your provider.

I'm not famililar enough with Nginx to provide multiple virtual-host examples for that.