首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >来自自定义http-片断的NGINX map指令变量是未知的。

来自自定义http-片断的NGINX map指令变量是未知的。
EN

Stack Overflow用户
提问于 2022-08-01 13:23:12
回答 1查看 411关注 0票数 1

问题:

当我尝试使用kubectl apply -f config.yaml应用下面的配置时,会打印以下错误消息:nginx: [emerg] unknown "name" variable,即使变量是由map-指令在相同的注释块/部分中定义的。

精化:

对于针对“hello-world”示例的每个请求,我希望解析查询参数“token”的前32个字符,以执行以下操作:

是我的新路径(rewrite-target)

  • ensure,使用这个32个字符的令牌的每个调用都被路由到同一个pod (upstream-hash-by)

因此,我创建了以下配置:

代码语言:javascript
运行
复制
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/http-snippet: |
      map $arg_token $name {
        default                "default";
        "^(?<short>.{32}).*$"  $short;
      }
    nginx.ingress.kubernetes.io/rewrite-target: /$name
    nginx.ingress.kubernetes.io/upstream-hash-by: $name
spec:
  rules:
    - host: hello-world.info
      http:
        paths:
          - path: /(.*)
            pathType: Prefix
            backend:
              service:
                name: nginx-deployment
                port:
                  number: 8080

例如,调用将是curl hello-world.info/example?token=1234567890abcdefghijhlmnopqrstuvwxyz。其思想是将存储在$arg_token中的36个字符长令牌缩减为1234567890abcdefghijhlmnopqrstuv (32个字符),将其存储在$name中,并使用这个新变量作为rewrite-targetupstream-hash-by ConfigMap资源的参数。

我的问题:

正如this在同一个上下文中回答一个问题时所写的那样,只应该在http-上下文中使用config-map指令,并且NGINX中的变量总是定义为全局的。为什么变量$name 仍然未知?

在自定义的http-snippet unknown 中定义的变量在访问.时会打印错误

编辑:完整堆栈-跟踪:

代码语言:javascript
运行
复制
Error from server (BadRequest): error when applying patch:
{"metadata":{"annotations":{"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"networking.k8s.io/v1\",\"kind\":\"Ingress\",\"metadata\":{\"annotations\":{\"nginx.ingress.kubernetes.io/http-snippet\":\"map $1 $name {\\n  default                 0;\\n  123*  \\t\\t\\t1;\\n}\\n\",\"nginx.ingress.kubernetes.io/rewrite-target\":\"/$name\",\"nginx.ingress.kubernetes.io/upstream-hash-by\":\"$name\"},\"name\":\"example-ingress\",\"namespace\":\"ingress-nginx\"},\"spec\":{\"rules\":[{\"host\":\"hello-world.info\",\"http\":{\"paths\":[{\"backend\":{\"service\":{\"name\":\"nginx-deployment\",\"port\":{\"number\":8080}}},\"path\":\"/(.*)\",\"pathType\":\"Prefix\"}]}}]}}\n","nginx.ingress.kubernetes.io/http-snippet":"map $1 $name {\n  default                 0;\n  123*  \t\t\t1;\n}\n","nginx.ingress.kubernetes.io/rewrite-target":"/$name","nginx.ingress.kubernetes.io/upstream-hash-by":"$name"}}}
to:
Resource: "networking.k8s.io/v1, Resource=ingresses", GroupVersionKind: "networking.k8s.io/v1, Kind=Ingress"
Name: "example-ingress", Namespace: "ingress-nginx"
for: "ingress-config.yaml": admission webhook "validate.nginx.ingress.kubernetes.io" denied the request:
-------------------------------------------------------------------------------
Error: exit status 1
2022/08/04 11:28:25 [warn] 263#263: the "http2_max_field_size" directive is obsolete, use the "large_client_header_buffers" directive instead in /tmp/nginx/nginx-cfg3947851792:143
nginx: [warn] the "http2_max_field_size" directive is obsolete, use the "large_client_header_buffers" directive instead in /tmp/nginx/nginx-cfg3947851792:143
2022/08/04 11:28:25 [warn] 263#263: the "http2_max_header_size" directive is obsolete, use the "large_client_header_buffers" directive instead in /tmp/nginx/nginx-cfg3947851792:144
nginx: [warn] the "http2_max_header_size" directive is obsolete, use the "large_client_header_buffers" directive instead in /tmp/nginx/nginx-cfg3947851792:144
2022/08/04 11:28:25 [warn] 263#263: the "http2_max_requests" directive is obsolete, use the "keepalive_requests" directive instead in /tmp/nginx/nginx-cfg3947851792:145
nginx: [warn] the "http2_max_requests" directive is obsolete, use the "keepalive_requests" directive instead in /tmp/nginx/nginx-cfg3947851792:145
2022/08/04 11:28:25 [emerg] 263#263: unknown "name" variable
nginx: [emerg] unknown "name" variable
nginx: configuration file /tmp/nginx/nginx-cfg3947851792 test failed
EN

回答 1

Stack Overflow用户

发布于 2022-09-19 08:21:53

我也面临着同样的问题,这就是我是如何解决这个问题的。

  1. 通过ConfigMap.

注入http块

代码语言:javascript
运行
复制
apiVersion: v1
kind: ConfigMap
metadata:
  name: ingress-nginx-controller
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
data:
  http-snippet: |
    map $arg_token $name {
        default                "default";
        "^(?<short>.{32}).*$"  $short;
    }

确保命名空间: ingress,因为它是k8s中入口控制器的默认名称空间

configuration-snippet

  1. 将重写移动到

代码语言:javascript
运行
复制
nginx.ingress.kubernetes.io/configuration-snippet: |
      rewrite "/(.*)" /$name break;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73194372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档