避免重複的 nginx 伺服器定義

避免重複的 nginx 伺服器定義

有沒有一種方法可以將兩個幾乎相同的伺服器定義合併為一個,但仍然具有不同的proxy_pass值,具體取決於伺服器名稱?

server_nameproxy_pass是兩者之間唯一的區別。

嘗試過

proxy_pass http://$server_name:8080;

但沒有成功。

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

答案1

將共享conf放入您的nginx目錄中的一個檔案中並包含它:

共享:

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

伺服器.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;
    }
}

相關內容