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

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, blog.example.com, git.example.com, etc..

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


Solution 1:

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:

/var/www/example.com
/var/www/sub.example.com

If you have subdomain files at /var/www/example.com/subdomain directory, you might have duplicate content issues, since the same content would be available at http://sub.example.com and http://example.com/subdomain.