How to structure a configuration file to avoid duplication

[Note: my question is not to do with reverse-proxy’s, it is about config but the template for creating a topic wouldn’t allow me to use config as a tag for this question for some reason].

What I’m trying to do:
I have a working site configuration file that creates a reverse-proxy and currently looks like this:

server {
        listen      80;
        listen [::]:80;
        listen      443 ssl;
        listen [::]:443 ssl;
        ssl_certificate        /path/to/cert.pem;
        ssl_certificate_key    /path/to/key.pem;
        ssl_client_certificate /path/to/ca.crt;
        ssl_crl                /path/to/ca.crl;
        ssl_verify_client on;

        root /var/www/mydomain.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name mydomain.com www.mydomain.com;

        location / {
                try_files $uri $uri/ =404;
        }
        location /a_thing/ {
                proxy_pass http://localhost:8888/;
        }
}

However, one of the sites I want to expose uses links with absolute paths that are not under my control. These break if I use a proxy_pass mapping based on path (e.g. a request to https//mydomain/a_thing might return a page with a link to /resource, which plainly won’t work as the site is not at the root, it is at /a_thing due to the proxying).

I can fix this by opening a separate port for this site that can be let through transparently at the root.

MY QUESTION: in doing this I do not want to duplicate the entire configuration; the majority of it is common, only the listen port and location changes.

Where I’m stuck:
How do I best go about structuring my site configuration file to cover multiple sets of listen/location while keeping everything else common?

What I’ve already tried:
Nothing yet: I see that the documentation talks of being hierarchical, maybe that is intended to be a hint?

FYI, what I decided to do in the end was to make my site configuration a directory rather than a file, i.e. /etc/nginx/sites-available/mydomain.com, put my common configuration into that directory in a file named common.cfg then move each listen/locationpair into a file of their own of the form /etc/nginx/sites-available/mydomain.com/mydomain.com_port_xxxx (each of which are individually symlinked into /etc/nginx/sites-enabled) and in those files I could then add a line

include /etc/nginx/sites-available/mydomain.com/common.cfg;

…for instance /etc/nginx/sites-available/mydomain.com/mydomain.com_port_5000 might contain:

server {
        listen      5000 ssl;
        listen [::]:5000 ssl;

        include /etc/nginx/sites-available/mydomain.com/common.cfg;

        location / {
                #  Pass traffic to SSH tunnel
                proxy_pass http://localhost:8888/;
        }
}

This seems to work nicely.