Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default path on multiple nginx ingress rewrite

Here is my situation, I'm on kubernetes (ingress), with two docker images: one dedicated to the web and the second one to the api.

Under the next configuration (at the end of the message): /web will show the front-end that will make some calls to /api, all good there.

but / is a 404 since nothing is defined, I couldn't find a way to tell in the ingress config that / should redirect to /web

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: dev-ingress
  annotations:
    kubernetes.io/tls-acme: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  tls:
  - hosts:
    - demo.com
    secretName: tls-secret
  rules:
  - host: demo.com
    http: 
      paths:
      - path: /api
        backend:
          serviceName: api-app
          servicePort: 8080
      - path: /web
        backend:
          serviceName: web-app
          servicePort: 80
like image 861
Miguel Angel Acevedo Avatar asked Aug 06 '18 07:08

Miguel Angel Acevedo


People also ask

What is rewrite target in ingress?

In this ingress definition, any characters captured by (. *) will be assigned to the placeholder $2 , which is then used as a parameter in the rewrite-target annotation. For example, the ingress definition above will result in the following rewrites: rewrite.bar.com/something rewrites to rewrite.bar.com/

What is nginx ingress default backend?

The default backend is a service which handles all URL paths and hosts the Ingress-NGINX controller doesn't understand (i.e., all the requests that are not mapped with an Ingress). Basically a default backend exposes two URLs: /healthz that returns 200. / that returns 404.

Is it possible to have multiple ingress controllers in the cluster?

You may deploy any number of ingress controllers using ingress class within a cluster. Note the . metadata.name of your ingress class resource.

What is pathType in ingress?

A new pathType field that can specify how Ingress paths should be matched. A new IngressClass resource that can specify how Ingresses should be implemented by controllers. Support for wildcards in hostnames.

What are Nginx rewrite rules and how to use them?

One of the most common uses of NGINX rewrite rules is to capture deprecated or nonstandard versions of a website’s domain name and redirect them to the current name. There are several related use cases.

What is ingress in Nginx?

The Ingress resource only allows you to use basic NGINX features – host and path-based routing and TLS termination. Thus, advanced features like rewriting the request URI or inserting additional response headers are not available. In addition to using advanced features, often it is necessary to customize or fine tune NGINX behavior.

How do I use regex in Nginx Plus?

Its syntax is simple enough: rewrite regex URL [flag]; But the first argument, regex, means that NGINX Plus and NGINX rewrite the URL only if it matches the specified regular expression (in addition to matching the server or location directive). The additional test means NGINX must do more processing.

How do I redirect to a directory in Nginx?

To indicate a directory, add a slash at the end of the element name. If none of the files or directories exist, NGINX performs an internal redirect to the URI defined by the final element (uri). For the try_files directive to work, you also need to define a location block that captures the internal redirect, as shown in the following example.


1 Answers

This depends on what your frontend and backend apps expect in terms of paths. Normally the frontend will need to be able to find the backend on a certain external path and in your case it sounds like your backend needs to be made available on a different path externally (/api) from what it works on within the cluster (/). You can rewrite the target for requests to the api so that /api will go to / when the request is routed to the backend:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: dev-ingress-backend
  annotations:
    kubernetes.io/tls-acme: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  tls:
  - hosts:
    - demo.com
    secretName: tls-secret
  rules:
  - host: demo.com
    http: 
      paths:
      - path: /api
        backend:
          serviceName: api-app
          servicePort: 8080

And you can also define a separate ingress (with a different name) for the frontend that does not rewrite the target, so that a request to /web will go to /web for it:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: dev-ingress-frontend
  annotations:
    kubernetes.io/tls-acme: "true"
spec:
  tls:
  - hosts:
    - demo.com
    secretName: tls-secret
  rules:
  - host: demo.com
    http: 
      paths:
      - path: /web
        backend:
          serviceName: web-app
          servicePort: 80
like image 74
Ryan Dawson Avatar answered Oct 20 '22 14:10

Ryan Dawson