【发布时间】:2021-12-18 13:02:37
【问题描述】:
我有一项托管在内部网络中的服务,该服务在自定义负载均衡器后面的端口 443(通过 https)中接收来自互联网和内部网络的流量。
内部网络请求带有一个额外的自定义标头,我们称之为X-my-lb-header。
我想阻止所有 uri 的所有外部传入流量(返回一个 http 响应代码),某些特定的除外。
例如,假设我想允许到达两个端点 /endpoind1/(前缀匹配)和 /endpoint2 实际匹配的流量。
实现这种行为的最佳方法是什么?
如果我的理解是正确的,我需要类似(下面的语法不正确)
location = /endpoind2 {
if ($http_x_my_lb_header not exists) {
pass
} else {
return 404
}
... the rest of the directives
}
location ~ / {
if ($http_x_my_lb_header) {
return 404;
}
... the rest of the directives
}
但是由于 nginx 不支持 else ,所以我想不通。
有什么想法吗?
【问题讨论】:
-
您的示例中根本不需要
else。将if ($http_x_my_lb_header) { return 404; }放在location块的开头将阻止没有X-my-lb-header的所有流量。但据我了解,您需要相反的东西? -
@IvanShatsky 确实我需要相反的。如果标头存在不要阻塞或说不同,如果标头不存在则阻塞。
标签: nginx networking nginx-config