kubernetes nginx 虚拟服务器子路由

时间:2021-04-28 09:04:50

标签: nginx kubernetes nginx-ingress

我对 kubernetes nginx 虚拟子路由有点困惑。 https://docs.nginx.com/nginx-ingress-controller/configuration/virtualserver-and-virtualserverroute-resources/#virtualserverroute-subroute

<块引用>

“在前缀的情况下,路径必须以与引用此资源的VirtualServer的路由路径相同的路径开头”

path: /coffee
action: 
    path: coffee

/coffee 会传递给应用吗?

因为当我尝试使用路由部署虚拟服务器时它不起作用(下面的示例)

path: /one
action: 
    path: hellok8s

但是,我以前使用的这条路线是有效的

path: /
action: 
    path: hellok8s

举个例子,如果我有一个 app-1 和 app-2...我应该通过主机还是通过子路径来区分它们?

  • app-1:helloworld.test.com
  • app-2:helloworld2.test.com

或者有什么方法可以通过下面的路径区分它们?

  • app-1:helloworld.test.com/appone
  • app-2:helloworld.test.com/apptwo

--- 编辑

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  name: hellok8s-app-vs
spec:
  host: helloworld.moonshot.com
  tls:
    secret: nginx-tls-secret
    # basedOn: scheme
    redirect:
      enable: true
      code: 301
      upstream:
  - name: hellok8s
    service: hellok8s-service
    port: 8080
  routes:
  - path: /one
    action:
      proxy:
        upstream: hellok8s
        rewritePath: /

1 个答案:

答案 0 :(得分:1)

所以路径是 Nginx 将暴露给外部世界的 URL。该路径在内部会发生什么取决于操作的子属性,一些示例:

此处的 /coffee 是最终用户看到的内容,但请求被发送到咖啡服务的根目录。因此,如果咖啡是在 8080 上运行的 K8S 中的服务,则请求将在 coffee:8080

path: /coffee
 action:
  pass: coffee

但是还有更多actions。假设您使用 action.proxy,那么您可以在更细粒度的级别定义路径应该发生的情况。因此,在下面的示例中,我们转发到 coffee 服务,但请求路径被重新写入 filtercoffee

proxy:
  upstream: coffee
  rewritePath: /filtercoffee 

你也可以使用redirect, return in action的pass指令,但是你必须use one of the four listed here