如何使用Nginx Ingress-Controller进行标头路由逻辑? [英] How to have a header routing logic with nginx ingress-controller?

查看:189
本文介绍了如何使用Nginx Ingress-Controller进行标头路由逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用nginx实现标头路由入口规则.为什么 ?因为相同的路径应该基于 headers 转到不同的后端.这是我尝试过的:

I'm trying to achieve a header routing ingress rule with nginx. Why ? Because the same path should go to different backend based on headers. Here what i've tried:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: api-mutli-back
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      set $dataflag 0;

      if ( $http_content_type ~ "multipart\/form-data.*" ){
      set $dataflag 1;
      }

      if ( $dataflag = 1 ){
      set $service_name "backend-data";
      }

spec:
  rules:
  - host: example.com
    http:
      paths:
      - backend:
          serviceName: backend-default
          servicePort: 80
        path: /api

但是nginx的日志输出此错误:

But the logs of nginx output this error:

unknown directive "set $service_name backend-data" in /tmp/nginx-cfg864446123:1237

对我来说这似乎不合逻辑...如果我检查nginx生成的配置,则每个规则都会在开头处生成一个类似以下内容的位置:

which seems unlogic to me... If I check the configuration generated by nginx, each rule generate a location with something like this at the begining:

[...]
       location ~* "^/api" {

            set $namespace      "my-namespace";
            set $ingress_name   "api-multi-back";
            set $service_name   "backend-default";
[...]

我在做什么错?是否无法通过注释 configuration-snippet 重新定义 service_name 变量?还有其他方法吗?

What am I doing wrong ? Isn't it possible to redefine service_name variable with annotation configuration-snippet ? Is there any other method ?

我在nginx方面的错误是由于 set $ service_name backend-data 之间缺少确切的空格.然后,nginx正确生成了配置,但仍未路由到另一个kubernetes服务.

My error on nginx side was due to the lack of exact spaces between set $service_name and backend-data. Then nginx generated correctly the configuration but it still not routing to another kubernetes service.

推荐答案

您被YAML主义所咬住:

You got bitten by a YAML-ism:

第二个if块的缩进与其他缩进不同,因此YAML认为您正在annotations:

The indentation of your 2nd if block isn't the same as the indentation of the others, and thus YAML thinks you are starting a new key under annotations:

你有

metadata:
  name: api-mutli-back
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      set $dataflag 0;

      if ( $http_content_type ~ "multipart\/form-data.*" ){
      set $dataflag 1;
      }

     if ( $dataflag = 1 ){
     set $service_name "backend-data"
     }

但是您应该拥有:

metadata:
  name: api-mutli-back
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      set $dataflag 0;

      if ( $http_content_type ~ "multipart\/form-data.*" ){
      set $dataflag 1;
      }

      if ( $dataflag = 1 ){
      set $service_name "backend-data"
      }

这篇关于如何使用Nginx Ingress-Controller进行标头路由逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆