如何使用子域作为参数/路径将域的自定义子域重定向到单个kubernetes服务? [英] How to redirect custom subdomain of a domain to a single kubernetes service with subdomain as parameter/ path?

查看:59
本文介绍了如何使用子域作为参数/路径将域的自定义子域重定向到单个kubernetes服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多租户应用程序的要求,其中每个租户都应由如下所示的子域分隔

I have a requirement for a multitenant application where each tenant should be separated by sub domains like below

t1.example.com  
t2.example.com  
t3.example.com  
.  
.  
.  
tn.example.com

因此,在这种情况下,可以有任意数量的租户.我有一个名为 myservice 的Kubernetes后端服务,负责处理所有这些请求,需要根据其子域进行标识. 例如如果请求来自tn.example.com,则需要将其重定向到-> myservice/tn .

So there could be any number of tenants in this case. I have a Kubernetes Backend Service named myservice responsible for handling all those requests and they need to be identified based on their subdomain. e.g. if the request comes from tn.example.com then it needs to be redirected to the -> myservice/tn .

t2.example.com   -> myservice/t2  
t3.example.com   -> myservice/t3 and so on.

因此,此处的子域名将是重定向期间的path参数,这将从应用程序角度区分不同的子域.我需要为任意数量的子域动态地执行此操作. 如何在Kubernetes Nginx入口控制器中实现这一目标?

So here the subdomain name will be the path argument during redirection which will differentiate different subdomains from app perspective . I need to do this dynamically for any number of subdomains. How this can be achieved in Kubernetes Nginx ingress controller ?

推荐答案

恐怕 Nginx Ingress 无法以简单明了的方式准确提供您所需的内容. 但是,您始终可以使用更多高级功能,例如使用Server/Location Block部分"rel ="nofollow noreferrer">通过注释配置代码段,然后使用 lua Block 提取subdomain并将请求URI更改为后端.

I'm afraid that Nginx Ingress cannot provide exactly what you need in easy and straightforward way. However, you can always use more advance features, like overriding Server/Location Block section with configuration snippet through annotation, and then use lua Block to extract subdomain and change the request URI to backend.

Github 上有类似的线程,其中用户sanigo使用了configuration-snippetlua block作为解决方法.

There was similar thread on Github, where user sanigo used configuration-snippet with lua block as workaround.

nginx.ingress.kubernetes.io/configuration-snippet: |
  location ~ ^/v2/ {
    set_by_lua_block $repo {
      local host = ngx.req.get_headers()["host"];
      local reg = "^(?<repo>[^.]+).*";
      local m = ngx.re.match(host, reg);
      return m['repo'];
    }
    rewrite ^/(.*)$ /repository/$repo/$1 last;
  }

快速注释::在此示例中,<repo>的作用与subdomain完全相同.

Quick note: in this example <repo> acts exactly as a subdomain.

这篇关于如何使用子域作为参数/路径将域的自定义子域重定向到单个kubernetes服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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