Nginx
如何共同重用 Nginx 指令
我在三個位置有一些共同的配置指令,所以與其重複指令,我寧願繼承共同的(代理記憶體設置和一些
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/; }