nginx-ingress支持多个路由前缀

时间:2019-08-13 14:00:03

标签: nginx kubernetes kubernetes-ingress nginx-ingress

我有一个service暴露在端口8888上,我正在尝试为ingress的服务创建一个path对象,以支持所有主机(不提及主机)。 我的问题是我的服务没有基本URL,并且它还有一些用于静态文件的路由。

我的入口文件:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/auth-realm: Authentication Required
    nginx.ingress.kubernetes.io/auth-secret: ingress-basic-auth-secret
    nginx.ingress.kubernetes.io/auth-type: basic
    nginx.ingress.kubernetes.io/enable-rewrite-log: "true"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/service-upstream: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/use-regex: "true"
  name: ingress-myapp
  namespace: default
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: myapp-service
          servicePort: 8888
        path: /myapp/?(.*)

与上面的ingress对象有关,我遇到了一个问题,因为我可以在<host ip>/myapp

上访问我的Web服务

但是我的某些静态文件(如徽标)无法正常工作,因为我的应用使用其他路由,并且浏览器获取的网址为:<host ip>/static/logo50x50.png,而我需要<host ip>/myapp/static/logo50x50.png

任何一种告诉Nginx入口者做前缀或重新写入我的路由的方法,以便Nginx入口者将转换URL: 从:<host ip>/static/logo50x50.png->到:<host ip>/myapp/static/logo50x50.png

这样我的客户(chrome)将获得:<host ip>/myapp/static/logo50x50.png

备注:我的http rule path对象中只需要使用1个http path rules而不是多个ingress,所以在nginx-ingress中使用类似path: /static/?(.*)的其他路径是没有帮助的我,因为我有更多具有相同路线/static/...

的豆荚

1 个答案:

答案 0 :(得分:0)

尝试将位置规则更改为- path: /(myapp)?/?(.*),然后将注释重写为:nginx.ingress.kubernetes.io/rewrite-target: /$2 所以看起来像这样:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/auth-realm: Authentication Required
    nginx.ingress.kubernetes.io/auth-secret: ingress-basic-auth-secret
    nginx.ingress.kubernetes.io/auth-type: basic
    nginx.ingress.kubernetes.io/enable-rewrite-log: "true"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/service-upstream: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/use-regex: "true"
  name: ingress-myapp
  namespace: default
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: myapp-service
          servicePort: 8888
        path: /(myapp)?/?(.*)

路径规则现在将匹配(几乎)任何内容。请寻找NGINX Ingress Controller的official documentation