Evite definição duplicada de servidor nginx

Evite definição duplicada de servidor nginx

Existe uma maneira de unir duas definições de servidor quase idênticas em uma, mas ainda com proxy_passvalores diferentes, dependendo do nome do servidor?

server_namee proxy_passsão as únicas diferenças entre os dois.

Tentei com

proxy_pass http://$server_name:8080;

mas sem sucesso.

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/;
    }
}

Responder1

Coloque o conf compartilhado em um arquivo no diretório nginx e inclua-o:

compartilhado:

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;
    }
}

informação relacionada