Search code examples
nginxkuberneteskubernetes-ingressmockserver

kubernetes nginx ingress controller rewrites


We have deployed a mockserver on kubernetes. Currently, we only have one hostname which is shared by couple other applications (using a different path). However, the dashboard is not working because of the css location. What's the best way to solve this problem?

Failed to load resource: the server responded with a status of 404 (), hostname/mockserver/dashboard/static/css/main.477cab2a.chunk.css

The ingress manifest:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    app.kubernetes.io/instance: mock-server
    kubernetes.io/ingress.class: nginx-ingress-protected
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: mock-server-ingress
  namespace: my-namespace
spec:
  rules:
    - host: hostname
      http:
        paths:
          - backend:
              serviceName: mock-server-svc
              servicePort: 80
            path: /testing(/|$)(.*)

This works fine if I request resource like hostname/testing/mockserver/expectation, the rewrites will be sending /mockserver/exepctation to the backend.

However, if for path hostname/testing/mockserver/dashboard, it is a html page which will loads hostname/mockserver/dashboard which doesn't exist. I can't wrap my head around this. Should I create another ingress with path /mockserver just to serve the css?


Solution

  • Your rewrite is working as expected. However, there are some options you can choose from:

    1. Create a second rule for the /mockserver (the simplest solution).

    2. Play with capture groups:

    Captured groups are saved in numbered placeholders, chronologically, in the form $1, $2 ... $n. These placeholders can be used as parameters in the rewrite-target annotation.

    1. Use a paid solution.

    The easiest would be to go for option 1 and create a second rule which would satisfy the path for the css.