我需要根据查询字符串参数限制对某些文件的访问。我有一个 NGINX 代理服务器,它位于其他几个 nginx Web 服务器的前面,用于负载平衡。我决定在代理服务器级别强制执行此查询字符串参数,以合并配置更改。这给我的设置增加了一点复杂性,因为请求不能被困在 if 中,因为它需要向上游发送。
server {
listen 443;
# SSL Settings
server_name staging.xxxx.com;
location / {
proxy_pass http://webdav-cluster;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
}
# Unless the correct application token is passed in as a query parameter
# then deny access.
location ~ \/protected\/.*txt$ {
if ($arg_secret != abc) {
return 403;
}
proxy_pass http://webdav-cluster;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
}
}
有没有办法将这 4 条代理线存储在一个位置或变量中,然后使用一条线在内部重定向到它?我也可以在不同的虚拟主机中使用相同的设置。
我知道这是一个老问题,但它是谷歌的最高结果,今天我发现了两个不需要
include
ing单独文件的替代方案。
作为一个稍微简单的示例,假设我希望
/route1
和 /route2
共享相同的代理设置。我可以配置正则表达式位置或使用 try_files
指令强制回退。
来源:https://serverfault.com/q/564127/805740
location ~ ^/(route1|route2) {
YOUR_PROXY_CONFIG_HERE
}
try_files
强制回退到共享代理配置来源:https://serverfault.com/a/1100562/805740
location /route1 {
try_files "" @fallback
}
location /route2 {
try_files "" @fallback
}
location @fallback {
YOUR_PROXY_CONFIG_HERE
}
location /route1 {
try_files "" @fallback
}
location ~ ^/(route2|route3) {
try_files "" @fallback
}
location @fallback {
YOUR_PROXY_CONFIG_HERE
}