我有三个位置有一些共同的配置指令,因此,与其重复这些指令,我宁愿继承公共的(代理缓存设置和一些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:根据真实案例编辑
答案1
您可以将上下文中的所有常用语句移至上下文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/;
}