Evite la definición duplicada del servidor nginx

Evite la definición duplicada del servidor nginx

¿Hay alguna manera de unir dos definiciones de servidor casi idénticas en una pero aún tener proxy_passun valor diferente que depende del nombre del servidor?

server_namey proxy_passson las únicas diferencias entre esos dos.

Probado con

proxy_pass http://$server_name:8080;

pero sin éxito.

server {
    listen              443 ssl;
    listen              [::]:443 ssl;
    server_name         localhost thevegcat.lan;
    root                e:/projects/TheVegCat/src/main/resources/static/;
    ssl_certificate     e:/projects/TheVegCat/ssl/CA/localhost/localhost.crt;
    ssl_certificate_key e:/projects/TheVegCat/ssl/CA/localhost/localhost.decrypted.key;
    add_header          Strict-Transport-Security "max-age=10368000; includeSubDomains" always;
    gzip                on;
    gzip_min_length     512;
    gzip_proxied        expired no-cache no-store private auth;
    gzip_types          text/plain application/xml text/css application/javascript application/json;
    location / {
        add_header      Strict-Transport-Security "max-age=10368000; includeSubDomains" always;
        proxy_pass      http://localhost:8080;
    }
    location ~ ^(.+)\.(ico|png|xml|json)$ {
        expires         1y;
        alias           e:/projects/TheVegCat/src/main/resources/static/favicon/;
        try_files       $1.$2 $1.$2/;
    }
}

server {
    listen              443 ssl;
    listen              [::]:443 ssl;
    server_name         veganskivodic.thevegcat.lan;
    root                e:/projects/TheVegCat/src/main/resources/static/;
    ssl_certificate     e:/projects/TheVegCat/ssl/CA/localhost/localhost.crt;
    ssl_certificate_key e:/projects/TheVegCat/ssl/CA/localhost/localhost.decrypted.key;
    add_header          Strict-Transport-Security "max-age=10368000; includeSubDomains" always;
    gzip                on;
    gzip_min_length     512;
    gzip_proxied        expired no-cache no-store private auth;
    gzip_types          text/plain application/xml text/css application/javascript application/json;
    location / {
        add_header      Strict-Transport-Security "max-age=10368000; includeSubDomains" always;
        proxy_pass      http://veganskivodic.thevegcat.lan:8080;
    }
    location ~ ^(.+)\.(ico|png|xml|json)$ {
        expires         1y;
        alias           e:/projects/TheVegCat/src/main/resources/static/favicon/;
        try_files       $1.$2 $1.$2/;
    }
}

Respuesta1

Coloque la configuración compartida en un archivo en su directorio nginx e inclúyalo:

compartido:

listen              443 ssl;
listen              [::]:443 ssl;
root                e:/projects/TheVegCat/src/main/resources/static/;
ssl_certificate     e:/projects/TheVegCat/ssl/CA/localhost/localhost.crt;
ssl_certificate_key e:/projects/TheVegCat/ssl/CA/localhost/localhost.decrypted.key;
add_header          Strict-Transport-Security "max-age=10368000; includeSubDomains" always;
gzip                on;
gzip_min_length     512;
gzip_proxied        expired no-cache no-store private auth;
gzip_types          text/plain application/xml text/css application/javascript application/json;
location ~ ^(.+)\.(ico|png|xml|json)$ {
    expires         1y;
    alias           e:/projects/TheVegCat/src/main/resources/static/favicon/;
    try_files       $1.$2 $1.$2/;
}

servidores.conf:

server {
    server_name         localhost thevegcat.lan;
    include shared;
    location / {
        add_header      Strict-Transport-Security "max-age=10368000; includeSubDomains" always;
        proxy_pass      http://localhost:8080;
    }
}

server {
    server_name         veganskivodic.thevegcat.lan;
    include shared;
    location / {
        add_header      Strict-Transport-Security "max-age=10368000; includeSubDomains" always;
        proxy_pass      http://veganskivodic.thevegcat.lan:8080;
    }
}

información relacionada