基本的auth / ip过滤器,仅用于Ingress NGINX中包含特殊字符的路径

Jen*_*ens 5 nginx basic-authentication kubernetes kubernetes-ingress nginx-ingress

我希望我的Ingress(NGINX)按源IP地址进行过滤,并在代理服务之前显示基本身份验证。尽管这很简单,但复杂的部分是,如果URL路径中包含特殊字符,我只希望它执行此操作。

可以说,在将它们代理到正确的服务之前,我想保护所有以“ +”开头的路径。另一方面,我仍然希望将不以“ +”开头的路径(没有基本身份验证)路由到同一服务。它还不应更改该服务将看到的URL。

例如:

/serviceA/what/ever -> http://192.168.0.2/what/ever
/serviceA/what/+ever -> BASIC_AUTH -> http://192.168.0.2/what/+ever
/serviceB/what/ever -> http://192.168.0.3/what/ever
/serviceB/+what/ever -> BASIC_AUTH -> http://192.168.0.3/+what/ever
Run Code Online (Sandbox Code Playgroud)

是否可以在Ingress或至少在NGINX配置中实现?URL路径的正则表达式在NGINX中也非常简单,但是是否可以在不复制所有路径条目的情况下也无需在前面添加第二个代理nginx呢?

理想的解决方案是在Ingress yml config中,但我对NGINX更为熟悉,因此这是我想在NGINX-Syntax中实现的示例:

Location ~ /+ {
    auth_basic ...;
    auth_basic_user_file ...;
    < route it somehow to the similar location as it would have no +, but don't cut out the + >
}
Location /serviceA {
    proxy_pass ...;
}
... more Locations ...
Run Code Online (Sandbox Code Playgroud)

或者在Ingress中,与路径条目类似。