我希望我的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
是否有可能在Ingress或至少在NGINX配置中实现? 在NGINX中,URL路径的正则表达式也非常简单,但是是否可以在不复制所有路径条目的情况下,也可以在前面不添加第二个代理nginx?
理想的解决方案是在Ingress yml配置中,但我对NGINX更为熟悉,所以这是我想在NGINX语法中实现的示例:
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 ...
或者在Ingress中,与路径条目类似。
答案 0 :(得分:0)
首先,您的:
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 + >
}
仅匹配servicex / + something,而不匹配servicex / something / + nice
您要搜索的正则表达式类似于:
location ~ ^/(.*)\+(.*) for the "+" to be anywhere
location ~ ^(.*)\/\+(.*) for the "+" to be only after a "/"
对于该部分:
< route it somehow to the similar location as it would have no +, but don't cut out the + >
就像这样,您将发送与它完全相同的uri:
proxy_pass http://192.168.0.2$request_uri;
这样,您将取出“ +”
proxy_pass http://192.168.0.2$1/$2;
其中$ 1是/ +之前的(。*),$ 2是其后的所有内容,我们在中间加上缺少的/。
我认为这将帮助您完成我想做的事情。
如果您有任何问题要问他们,我觉得您的解释有些迷茫,我的回答不是100%正确。
希望我能帮上忙。
答案 1 :(得分:0)
您can use nginx regexes在Yaml路径字段中。
要在一个而不是另一个中进行身份验证,您必须创建2个入口(因为auth注释是按入口而不是按路径)。同一主机有2个入口完全可以,nginx-ingress会将它们合并。
类似的事情应该起作用:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: without-auth
spec:
rules:
- host: example.com
http:
paths:
- path: /
backend:
serviceName: your-backend-service
servicePort: 80
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: with-auth
annotations:
# type of authentication
nginx.ingress.kubernetes.io/auth-type: basic
# name of the secret that contains the user/password definitions
nginx.ingress.kubernetes.io/auth-secret: basic-auth
# message to display with an appropriate context why the authentication is required
nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - foo'
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: example.com
http:
paths:
- path: ^/+
backend:
serviceName: your-backend-service
servicePort: 80
要调试它是否不起作用或检查结果是否符合您的期望,您可以像这样查看生成的nginx配置(用您的替换名称空间和pod名称)。
kubectl -n nginx-ingress exec nginx-ingress-controller-76458655df-sddg2 -- cat /etc/nginx/nginx.conf