Nginx server blocks: subdomains vs paths
Solution 1:
You can tell NGINX to respond to all requests on an IP address or port if you want. Subdomains/domains in the configuration just limit the responses from a specific "server" configuration block.
Location has nothing to do with domains/subdomains except that they live off of whatever is configured in the server block. Generally for NGINX, the first thing you will configure your domains and IP for listening and then forget about that completely.
Your first example sets up xxxx.yyy.com
=> http://127.0.0.1:3000
Second example sets up yyy.com/xxxx
=> http://127.0.0.1:4000
You can combine example #1 into example #2 by putting both base domain and subdomain in #2's server block (if you don't care that both paths will work for both the base domain and subdomain):
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name yyy.com xxxx.yyy.com;
location / {
proxy_pass http://127.0.0.1:3000;
}
location /xxxx/ {
proxy_pass http://127.0.0.1:4000;
}
}
So you can see there is no "separation at the DNS" as you say -- unless you host these on different IP addresses. Your only separation is in the paths.
The combined example produces:
xxxx.yyy.com
=> http://127.0.0.1:3000
yyy.com
=> http://127.0.0.1:3000
yyy.com/xxxx
=> http://127.0.0.1:4000
xxxx.yyy.com/xxxx
=> http://127.0.0.1:4000
Subdomains are not paths. They are entirely different things. So trying to compare and contrast them suggests you are way off track in your understanding.
Perhaps you have had a subdomain somewhere in the past where you had one directory for a subdomain and another directory for the main domain. These are your docroots, and this is just the way some web based server admin tools configure things by default. There is no inherent association between subdomains and paths unless you (or some tool or sysadmin) creates one.
You can map any domain or subdomain -- or any path you set in a location block --to /any/path/you/want in your filesystem.
Location is all about PATHS... and here again, after NGINX has found a request that matches the server block, then it is watching for requests that match the path in the HTTP request.
/thispath /thatpath /foo /bar or /foo/bar/on/thatpath or you can use wildcards and regular expressions. You can configure different file extensions to be served from different directories, or a common use of location is to tell NGINX how to handle certain file types like PHP.
Several example configurations are given here: http://nginx.org/en/docs/beginners_guide.html
If you find this helpful, please upvote or mark as a solution. Thanks!
Solution 2:
Since the topic is nginx configuration, there is no dns level separation if both domains point to the same IP/server.
The difference is, in which domains are which paths available/handled.
You can configure multiple domains with the same locations but one domain different. That's the main use of server_name
.
location
blocks are that part of the URL after the domain name/IP.http://www.example.com/location/
You can make different local paths available in different location blocks/URLs.
What you can achieve with dns is different from nginx configuration.
DNS round robin makes load balancing and high availability possible.
Geo location based resolving allows faster delivery of content.
It really depends on what you want to achieve.