How should I set my nginx configuration to use subdomains with CNAME host record?

cn flag

running Debian 11.

How should I add my subdomain to my site using my CNAME DNS records? My website directory is located at /var/www/ and I'm wondering if I can make a subdirectory for the subdomain within that directory.

i.e. /var/www/domain-dir/subdomain-dir

Can I add subdomain server_name to right in my sites-available config file?

I don't want to create tons of A/AAAA records to point to my subdomain as it could become messy as I'll likely create more subdomains later,,, etc..

Sorry if this post is long, I'm new to this and don't know how to make it more concise!

us flag

The actual directory in nginx can be anything, you just need to set the root directive properly in the virtual host for the subdomain.

I would advice to have a completely separate directory for each virtual host, for example:


If you have subdomain files at /var/www/ directory, you might have duplicate content issues, since the same content would be available at and

ie flag
  1. Add the sub domains as entries in the same server config file and point the sub domains to the specific file path containing the sub domains files. Creating a sub directory should work.

  2. Update cnames with your dns provider.

You can use nginx -t to check the syntax in you files.


Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.