0

I have a web application running on Kubernetes, and an nginx-ingress which serves as the Ingress for the deployment.

I'd like to enable the following behavior for nginx: when any request receives a HTTP 404 response from the origin, proxy the request to a second, static source.

I know how to enable that behavior on a standard nginx deployment, but how can it be done using nginx-ingress with minimal effort (i.e. providing a full custom configuration file is something I prefer to avoid)

1 Answer 1

0

When using nginx-ingress you can use default backend annotation. In documentation you can read:

This annotation is of the form nginx.ingress.kubernetes.io/default-backend: < svc name > to specify a custom default backend. This < svc name > is a reference to a service inside of the same namespace in which you are applying this annotation. This annotation overrides the global default backend.

This service will be handle the response when the service in the Ingress rule does not have active endpoints. It will also handle the error responses if both this annotation and the custom-http-errors annotation is set.

So you can just create your default service pointing to some backend (pod) serving your static website

So your ingress now should look like this:

---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    nginx.ingress.kubernetes.io/default-backend: default-backend
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: myservice
          servicePort: 80

and your default service like this:

---
apiVersion: v1
kind: Service
metadata:
  name: default-backend
spec:
  selector:
      app: myapp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

For accessing external static resources I tried using service of type ExternalService but it seems to crash my nginx-ingress-controller pod so I wouldn't recommend using it. If you want to access external resources (outside of cluster) create pod that will proxy requests to this external service and point your ingress's default-backend to its service.

Let me know if this helped

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .