Tuesday, February 7, 2023

proxy_pass vs. rewrite in nginx ingress controller

 

proxy_pass rewrite
Return 200 with the content 302 with the URL to the redirected page
Content To nginx, then the client Second call skips nginx; returned through the redirect URL directly to client

Example of proxy_pass:

location /testing {
  proxy_pass https://news.yahoo.com/us;
}

Example of rewrite:

rewrite ^/(.*)$ https://www.newurl.com/$1-$http_x_header1 redirect
How to hook up them to nginx conf?

Add them as annotations to the Ingress. One example is:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress-name
  annotations:
    nginx.org/mergeable-ingress-type: "master"
    nginx.org/server-snippets: |
      rewrite ....
      location /testing {
        ....
      }
spec:
  ....


Annotation of nginx.org/rewriters:

nginx.org/rewriters: "serviceName=my-service-name rewrite=/my-new-endpoint" 

Monday, January 30, 2023

Ingress to support multiple hosts

The Nginx ingress controller allows you to specify ingress in the helm chart.  For each ingress, you define the ingress rules in the corresponding yaml file.  You can specify each host and its rules. 

But to make the ingress dynamically apply the same rules for multiple hosts, you will need to specify the list of hosts from values.yaml file.  In this case, you are not allowed to pass a list of hosts as comma-separated hosts or other delimiter-separated hosts.  Even all hosts follow a pattern, the wild card is not supported.

To allow you to pass a list of hosts in values.yaml file, there is a way to do this.

In your values.yaml file, you pass the hosts like this: 

ingress:
  dnshosts:
    - host-1
    - host-2

 

  In your ingress yaml file, you use 'range' to iterate each host:

{{- range .Values.ingress.dnshosts }}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress-service1-{{ . | quote | sha1sum | trunc 5}}
  namespace: {{ template "my.namespace" $ }}
  annotations:
    nginx.org/mergeable-ingress-type: "minion"
spec:
  ingressClassName: "nginx"
  rules:
  - host: {{ . | quote }}
    http:
      paths: 
      - path: /service1-endpoint
        pathType: Prefix
        backend:
          service:
            name: service1
            port:
              number: 80
{{- end }}


Reference: