Nginx

如何共同重用 Nginx 指令

  • September 10, 2022

我在三個位置有一些共同的配置指令,所以與其重複指令,我寧願繼承共同的(代理記憶體設置和一些add_header指令)並且只寫差異(proxy_pass在我的情況下):

server {
   server_name   mypage.com;
   listen        443 ssl http2 default_server;
   listen        [::]:443 ssl http2 default_server;
   root          /opt/myApps/mypage/;
   index         index.jsp;
   charset       utf-8;

   location /data/a/ {
       proxy_cache             azure_cache;
       proxy_http_version      1.1;
       proxy_set_header        Host accountname.blob.core.windows.net;
       proxy_hide_header       "Set-Cookie";
       proxy_ignore_headers    "Set-Cookie";
       proxy_cache_revalidate  on;
       proxy_intercept_errors  on;
       proxy_cache_use_stale   error timeout updating http_500 http_502 http_503 http_504;
       proxy_cache_lock        on;
       proxy_cache_valid       200 304 30d;
       proxy_cache_valid       404 10m;
       add_header              Cache-Control max-age=31536000;
       add_header              X-Cache-Status $upstream_cache_status;
       proxy_pass              https://accountname.blob.core.windows.net/a/;
   }

   location /data/b/ {
       proxy_cache             azure_cache;
       proxy_http_version      1.1;
       proxy_set_header        Host accountname.blob.core.windows.net;
       proxy_hide_header       "Set-Cookie";
       proxy_ignore_headers    "Set-Cookie";
       proxy_cache_revalidate  on;
       proxy_intercept_errors  on;
       proxy_cache_use_stale   error timeout updating http_500 http_502 http_503 http_504;
       proxy_cache_lock        on;
       proxy_cache_valid       200 304 30d;
       proxy_cache_valid       404 10m;
       add_header              Cache-Control max-age=31536000;
       add_header              X-Cache-Status $upstream_cache_status;
       proxy_pass              https://accountname.blob.core.windows.net/b/;
   }

   location /data/c/ {
       proxy_cache             azure_cache;
       proxy_http_version      1.1;
       proxy_set_header        Host accountname.blob.core.windows.net;
       proxy_hide_header       "Set-Cookie";
       proxy_ignore_headers    "Set-Cookie";
       proxy_cache_revalidate  on;
       proxy_intercept_errors  on;
       proxy_cache_use_stale   error timeout updating http_500 http_502 http_503 http_504;
       proxy_cache_lock        on;
       proxy_cache_valid       200 304 30d;
       proxy_cache_valid       404 10m;
       add_header              Cache-Control max-age=31536000;
       add_header              X-Cache-Status $upstream_cache_status;
       proxy_pass              https://accountname.blob.core.windows.net/c/;
   }

   location / {
       # Other stuff here...
   }
}

proxy_pass請注意指令中的差異。

有什麼辦法可以更清楚地做到這一點嗎?

PS:用真實例子編輯

您可以將所有常用語句從location上下文移動到server上下文中。有關詳細資訊,請參閱此文件

您的塊中有一組相同的add_header語句location,因此這些語句也可以移動到server上下文中。有關詳細資訊,請參閱此文件

從技術上講,該proxy_set_header Host accountname.blob.core.windows.net;語句是多餘的,因為預設值取自proxy_pass相同的值。

例如:

proxy_cache             azure_cache;
proxy_http_version      1.1;
proxy_set_header        Host accountname.blob.core.windows.net;
proxy_hide_header       "Set-Cookie";
proxy_ignore_headers    "Set-Cookie";
proxy_cache_revalidate  on;
proxy_intercept_errors  on;
proxy_cache_use_stale   error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock        on;
proxy_cache_valid       200 304 30d;
proxy_cache_valid       404 10m;

add_header              Cache-Control max-age=31536000;
add_header              X-Cache-Status $upstream_cache_status;

location /data/a/ {
   proxy_pass https://accountname.blob.core.windows.net/a/;
}

location /data/b/ {
   proxy_pass https://accountname.blob.core.windows.net/b/;
}

location /data/c/ {
   proxy_pass https://accountname.blob.core.windows.net/c/;
}

但是,還有進一步的簡化,因為值的結尾在所有三種情況下都與location值的結尾相匹配,proxy_pass並且在其他方​​面是相同的。因此,您可以通過將三個位置減少到一個塊中來修剪它們以獲得相同的結果。有關詳細資訊,請參閱此文件

例如:

location /data/ {
   proxy_pass https://accountname.blob.core.windows.net/;
}

引用自:https://serverfault.com/questions/1110359