apiVersion: extensions/v1beta1
kind: Ingress
metadata:
namespace: default
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: $2
spec:
rules:
- host: hostname.com
http:
paths:
- path: /
backend:
serviceName: frontend
servicePort: 80
- path: /api/v1(/|$)(.*)
backend:
serviceName: backend
servicePort: 80
What I am trying to accomplish here is:
hostname.com/api/v1/anyurl should become hostname.com/anyurl when it goes to the backend.
hostname.com/anyurl should remain hostname.com/anyurl and go to the frontend.
The /api/v1 rewrite seems to work, but any urls going to the frontend gets rewrited to /.
What I need is the rewrite rule to only apply to the /api/v1 path
I guess this should work for you -
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
namespace: default
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: $1
spec:
rules:
- host: hostname.com
http:
paths:
- path: /(.*)
backend:
serviceName: frontend
servicePort: 80
- path: /api/(.*)
backend:
serviceName: backend
servicePort: 80
I have just edited this, it works for me, please check for this. I guess we can troubleshoot
Related
I am struggling with making a rewrite/redirect rule on nginx-ingress on Kubernetes.
According to the doc https://kubernetes.github.io/ingress-nginx/examples/rewrite/ it says it is possible with the annotation "nginx.ingress.kubernetes.io/rewrite-target", but unable to implement on my ingress. Maybe I am doing it wrong.
An example, portion of nginx-ingress yaml file.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
example.com/deployment-name: frontend-page
example.com/ingress-hostnames: www.example.com
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- host: www.example.com
http:
paths:
- backend:
serviceName: frontend-page
servicePort: 8080
path: /front(/|$)(.*)
Tried another way around as well but no luck:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
example.com/deployment-name: frontend-page
example.com/ingress-hostnames: www.example.com
nginx.ingress.kubernetes.io/rewrite-target: /shop
spec:
rules:
- host: www.example.com
http:
paths:
- backend:
serviceName: frontend-page
servicePort: 8080
path: /
I want to redirect any traffic to the page www.example.com/front/checkdomain to www.example.com/checkdomain. I want to remove that "front" path comes in between.
try this with an updated API version, hope your ingress supports networking.k8s.io/v1beta1
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: rewrite
namespace: default
spec:
rules:
- host: rewrite.bar.com
http:
paths:
- backend:
serviceName: http-svc
servicePort: 80
path: /something(/|$)(.*)
Example : https://kubernetes.github.io/ingress-nginx/examples/rewrite/#examples
The solution was
Changing from:
nginx.ingress.kubernetes.io/rewrite-target: /$2
To:
nginx.ingress.kubernetes.io/rewrite-target: /$2/
So the yaml looks like:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
example.com/deployment-name: frontend-page
example.com/ingress-hostnames: www.example.com
nginx.ingress.kubernetes.io/rewrite-target: /$2/
spec:
rules:
- host: www.example.com
http:
paths:
- backend:
serviceName: frontend-page
servicePort: 8080
path: /front(/|$)(.*)
I have two apps (workloads): app1 and app2. My goal is to redirect trafic from app1 to app2 if there is the diona prefix. For example:
app1/diona > app2/<api>
Now I have such rules:
rules:
- host: host
http:
paths:
- backend:
serviceName: ingress-37ce1ad1e8214375784d1e50805c056c
servicePort: 80
path: /diona
When I check app1/diona endpoint in logs of app2 there is an error:
Not Found: /diona
How can I redirect trafic correctly without realizing diona prefix in app2?
You can achieve it using nginx ingress controller. You code will look like this
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
name: rewrite
spec:
rules:
- host: host
http:
paths:
- backend:
serviceName: ingress-37ce1ad1e8214375784d1e50805c056c
servicePort: 80
path: /diona
If you want to redirect app1/diona/* to app2/*, change annotations to
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
name: rewrite
spec:
rules:
- host: host
http:
paths:
- backend:
serviceName: ingress-37ce1ad1e8214375784d1e50805c056c
servicePort: 80
path: /diona/(.*)
Reference
I have a react app that works fine. However after putting it in k8s and installing ingress, whenever I refresh a page i get the following although it just refreshed the page. How do I correct this?
Here is a snippet of my yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- http:
paths:
- path: /api/auth/*
backend:
serviceName: um-service
servicePort: 5001
- path: /api/profile/*
backend:
serviceName: um-service
servicePort: 5001
- path: /api/pass/*
backend:
serviceName: um-service
servicePort: 5001
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: admin-ingress
annotations:
kubernetes.io/ingress.class: nginx
# nginx.ingress.kubernetes.io/use-regex: "true"
# nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /admin
# pathType: Exact
backend:
serviceName: admin-service
servicePort: 4000
I must say I have challenges hitting the admin backend. It just doesn't load i.
I'm using ingress for kubernetes. My ingress is kubernetes nginx ingress. And routing configs don't work and redirect me root path in every request in https section All configs is given below
cat frontapi-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: swagger-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: preprod-ops.kblab.local
http:
paths:
- backend:
serviceName: kb-workplace
servicePort: 8080
path: /
- backend:
serviceName: gw-branch
servicePort: 8443
path: /api
$ cat swagger-portal.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: swagger-portal
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: preprod-ops.kblab.local
http:
paths:
- backend:
serviceName: swagger-portal
servicePort: 9001
path: /swagger-portal
Above config works properly with http config but, below config doesn't work with https config. It only works swagger-portal section
$cat frontapi-ingress-https.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
ingress.kubernetes.io/secure-verify-ca-secret: preprod.kblab.local
kubernetes.io/ingress.class: nginx
generation: 1
name: app-ingress
spec:
rules:
- host: preprod.kblab.local
http:
paths:
- backend:
serviceName: gw-branch
servicePort: 8443
path: /api
- backend:
serviceName: kb-workplace
servicePort: 8080
path: /
- backend:
serviceName: acs-alfresco
servicePort: 8080
path: /acs
tls:
- hosts:
- preprod.kblab.local
- secretName: preprod.kblab.local
$ cat swagger-portal-https.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
name: swagger-portal
spec:
rules:
- host: preprod.kblab.local
http:
paths:
- backend:
serviceName: swagger-portal
servicePort: 9001
path: /swagger-portal/a
tls:
- hosts:
- preprod.kblab.local
- secretName: preprod.kblab.local
Thanks in advance.
You are setting the nginx.ingress.kubernetes.io/rewrite-target: / annotation on your swagger-portal-https.yaml Ingress, which explicitly implies the redirection to the root path.
If you want to preserve the request path, you need to remove this annotation.
I'm trying to make a simple example of ingress-nginx on google cloud, but it's not matching the subpaths:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /one
backend:
serviceName: test-one-backend
servicePort: 80
- path: /two
backend:
serviceName: test-two-backend
servicePort: 80
When I call, http://server/one works, but when I call http://server/one/path I get a 404.
I'd tried several things like using regex, but is simply not working
The backends are just, echo servers that reply always on any path.
You need to use a /* at the end of the path:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /one/*
backend:
serviceName: test-one-backend
servicePort: 80
- path: /two
backend:
serviceName: test-two-backend
servicePort: 80
It's not really documented widely as of today, but in essence the path translates to a location {} block in the nginx.conf
Attention it changed in version 0.22.0 of ingress-nginx. checkout example at https://github.com/kubernetes/ingress-nginx/blob/master/docs/examples/rewrite/README.md
Now you have to work with captured groups to pass a subpath to the rewrite-target.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: rewrite
namespace: default
spec:
rules:
- host: rewrite.bar.com
http:
paths:
- backend:
serviceName: http-svc
servicePort: 80
path: /something(/|$)(.*)
This definition matches the exact string for the path. If you want to include subdirectories you can use the wildcard *. Your modified definition should look like this:
- path: /one/*
backend:
serviceName: test-one-backend
servicePort: 80
I am not sure if http://server/one (without the slash at the end) still works. In that case you have to keep your old definition and add another one like the above.