중복된 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;
    }
}

관련 정보