Vermeiden Sie doppelte Nginx-Serverdefinitionen

Vermeiden Sie doppelte Nginx-Serverdefinitionen

Gibt es eine Möglichkeit, zwei nahezu identische Serverdefinitionen zu einer zusammenzufassen, proxy_passdie aber je nach Servernamen dennoch unterschiedliche Werte aufweisen?

server_nameund das proxy_passsind die einzigen Unterschiede zwischen den beiden.

Versucht mit

proxy_pass http://$server_name:8080;

aber kein Erfolg.

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

Antwort1

Legen Sie die gemeinsame Konfiguration in einer Datei in Ihrem Nginx-Verzeichnis ab und schließen Sie sie ein:

geteilt:

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

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

verwandte Informationen