繁体   English   中英

Nginx 入口阻止所有路径 (/*) 并仅允许某些特定路径(/code-refiner/ 和 /v1/unsubscribed/*)

[英]Nginx Ingress block all path (/*) and allow only some specific path (/code-refiner/ and /v1/unsubscribed/*)

我想创建一个 nginx 入口,它只允许用户连接几条路径,而 rest 全部阻止或提供 403 错误。 有什么方法可以实现吗?

我只希望用户允许连接“/code-refiner/”,/v1/unsubscribed/* 和 rest 都应该被阻止。

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: code-refiner-service-ingress-external
  namespace: backend
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx-external
spec:
  rules:
  - host: code-refiner.example.com
    http:
      paths:
      - backend:
          service:
             name: code-refiner-service
             port:
               number: 80
        path: /
        pathType: Prefix

我需要实现这样的目标

location /* {
       deny all;
      }
location /code-refiner/ or /v1/unsubscribed/{
            allow all;
      }

根据这个git 链接,您可以创建两个 Ingress 并且只将注释添加到具有您要保护的路径的 ingress

  1. 对于您的问题,默认情况下首先创建两个 Ingresses,没有任何限制。
  2. 然后,按照文档中的描述为 auth 创建一个秘密。(创建一个 htpasswd 和秘密)

创建 htpasswd

$ htpasswd -c auth foo 
New password: <bar> 
New password: \
Re-type new password: 
Adding password for user foo

创建秘密:

 kubectl create secret generic basic-auth --from-file=auth secret

3.Second Ingress 对您需要限制的路径进行身份验证。

kind: Ingress
apiVersion: extensions/v1beta1
metadata:
annotations:
kubernetes.io/ingress.class: nginx
ingress.kubernetes.io/rewrite-target: /
kubernetes.io/tls-acme: true
# type of authentication
ingress.kubernetes.io/auth-type: basic
# name of the secret that contains the user/password definitions
ingress.kubernetes.io/auth-secret: basic-auth
# message to display with an appropiate context why the authentication is required
ingress.kubernetes.io/auth-realm: "Authentication Required - foo"
# Below configuration-snippet is to pass on the authenticated user-name to serviceB
ingress.kubernetes.io/configuration-snippet: |
proxy_set_header X-AUTH-USER $remote_user;
name: my-nginx-ingress-auth
spec:
tls:    
     hosts:
          myhost
          secretName: mysecret
          rules:
     host: myhost
     http:
     paths:
         path: /serviceB/
         backend:
         serviceName: serviceB-service
         servicePort: 7070

添加这些堆栈链接[1] [2]供您参考。

第二个是使用ConfigMapsServer-snippet

你所要做的就是找到你的 configMap:

kubectl get pod <nginx-ingress-controller> -o yaml

这位于容器参数中:

spec:
      containers:
       - args:
              - /nginx-ingress-controller
               - configmap=$(POD_NAMESPACE)/nginx-loadbalancer-conf

然后只需编辑它并放置添加服务器片段部分

apiVersion: v1 
data: server-snippet:  | 
location /admin-access { 
deny all; 
}

这种方法允许您为 Ingress 资源中定义的所有主机全局定义受限位置。

请注意,使用服务器片段时,无法在入口资源 object 中定义您阻止的路径。但是,还有另一种通过 ConfigMap 使用位置片段的方法:

location ~* "^/web/admin { 
deny all; 
}

对于入口 object 中的每个现有路径,都会有入口规则,但它将针对特定的 uri 被阻止(在上面的示例中,当管理员出现在 web 之后时,它将被阻止)。 所有其他 uri 都将通过。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM