How to write a DRY, modular nginx conf (reverse proxy) with named locations

Solution 1:

I've solved similar problem using nginx map feature.

First create a domain name to backend map:

map $http_host $backend {
  myhost1.tld 192.168.1.100;
  myhost2.tld 192.168.1.101;
  default     upstream_pool1;
}

then use map in location

location / {
  common settings
  proxy_pass $backend; 
}

You can use any other variable instead of $http_host See this manual: http://nginx.org/en/docs/http/ngx_http_map_module.html

Solution 2:

Is there a way to "call" a named location directly?!

There is one more way at least:

location /somelocation {
    error_page 418 = @named_location;
    return 418;
}