问题原文
原帖链接 · Ommadawn · CC BY-SA 4.0
I have three locations with some configuration directives in common, so rather than repeat the directives, I'd prefer to inherit the common (proxy cache settings and some add_header directives) and only write the difference (proxy_pass, in my case):
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...
}
}
Note the difference in the proxy_pass directive.
Is there any way to do that clearer?
PS: edited with real example
已采纳解答
原帖链接 · Richard Smith · CC BY-SA 4.0
You can move all of the common statements from the location context into the server context. See this document for details.
You have an identical set of add_header statements in your location blocks, so these too can be moved into the server context. See this document for details.
Technically, the proxy_set_header Host accountname.blob.core.windows.net; statement is redundant as the default value is taken from the proxy_pass value which is identical.
For example:
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/;
}
However, there is a further simplification as the end of the location values match the end of the proxy_pass values in all three cases and are otherwise identical. So, you can trim these back to achieve the identical result by reducing the three locations into a single block. See this document for details.
For example:
location /data/ {
proxy_pass https://accountname.blob.core.windows.net/;
}
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
