I am trying to write an ingress for a rest api service but I am seeing "default backend 404"
my rest service url is like this
https://myurl.com/restsvc/api/test/v1/app/hello?name=bob
below is my ingress yml:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: restservice-ingress
annotations:
kubernetes.io/ingress.class: nginx
spec:
tls:
- hosts:
- myurl.com
secretName: secret
rules:
- host: myurl.com
http:
paths:
- path: /restsvc/api/test/v1/app/(.*)
pathType: Prefix
backend:
serviceName: restservice-svc
servicePort: 8080
Any help appreciated, Thank you
try this -
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
kubernetes.io/ingress.class: nginx
name: staging-ingress
spec:
rules:
- host: myurl.com
http:
paths:
- path: /restsvc/api/test/v1/app/(.*)
backend:
serviceName: restservice-svc
servicePort: 8080
tls:
- hosts:
- myurl.com
secretName: secret
Fixed this by using the below rewrite target
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
kubernetes.io/ingress.class: nginx
name: staging-ingress
spec:
rules:
- host: myurl.com
http:
paths:
- path: /restsvc(/|$)(.*)
backend:
serviceName: restservice-svc
servicePort: 8080
tls:
- hosts:
- myurl.com
secretName: secret
Related
currently I'm trying the following setup:
I have:
one cluster
one Ingress Controller
one url (myapp.onazure.com)
two namespaces for two applications default and default-test
two deployments, ingress objects, services for the namespaces
I can easily reach my app from the default namespace with path based routing '/' as a prefix rule
Now i have tried to configure the second namespace and following rule: /testing to hit another service
Unfortunately i get an HTTP404 when i try to hit the following URL myapp.onazure.com/testing/openapi.json
What did I miss?
Working Ingress 1
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: liveapi-ingress-object
namespace: default
annotations:
kubernetes.io/ingress.class: public-nginx
spec:
tls:
- hosts:
- myapp-region1.onazure.com
- myapp-region2.onazure.com
secretName: ingress-tls-csi
rules:
- host: - myapp-region1.onazure.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: liveapi-svc
port:
number: 8080
- host: myapp-region2.onazure.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: liveapi-svc
port:
number: 8080
Not working Ingress 2
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: liveapi-ingress-object-testing
namespace: default-testing
annotations:
kubernetes.io/ingress.class: public-nginx
#nginx.ingress.kubernetes.io/rewrite-target: /testing
spec:
tls:
- hosts:
- myapp-region1.onazure.com
- myapp-region2.onazure.com
secretName: ingress-tls-csi-testing
rules:
- host: myapp-region1.onazure.com
http:
paths:
- path: /testing
#pathType: Prefix
backend:
service:
name: liveapi-svc-testing
port:
number: 8080
- host: myapp-region2.onazure.com
http:
paths:
- path: /testing
#pathType: Prefix
backend:
service:
name: liveapi-svc-testing
port:
number: 8080
Maybe I am missing a rewrite target to simply '/' in the testing namespace ingress?
Finally I figured out the missing part. I had to add the following statement to the not working ingress object:
annotations:
kubernetes.io/ingress.class: public-nginx
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$1
Please see the complete ingress object:
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: liveapi-ingress-object
namespace: default-testing
annotations:
kubernetes.io/ingress.class: public-nginx
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
tls:
- hosts:
- myapp.onazure.com
secretName: ingress-tls-csi-testing
rules:
- host: myapp.onazure.com
http:
paths:
- path: /testing/(.*)
pathType: Prefix
backend:
service:
name: liveapi-svc-testing
port:
number: 8000
Use full DNS name of a service,
$SERVICE.$NAMESPACE.svc.cluster.local
But you need to make sure your ignress controller has acccess to the desired namespace.
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 tomcat backend service running on kubernetes cluster try to rewrite using ingress with path /blob/api/v1/test-backend > /api/v1/test-backend so
the configuration now is running so can hit to xx.somedomain.com/blob/api/v1/test-backend and i want change to xx.somedomain.com/api/v1/test-backend with rewrites
my basic ingress
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-blob
namespace: blob-test
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "true"
nginx.ingress.kubernetes.io/proxy-body-size: 50m
spec:
tls:
- hosts:
- xx.somedomain.com
secretName: cert-key
rules:
- host: xx.somedomain.com
http:
paths:
- path: /blob/
backend:
serviceName: blob-service
servicePort: 8080
- path: /
backend:
serviceName: web-service
servicePort: 80
and this is yaml for rewrite /blob/
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-blob
namespace: blob-test
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "true"
nginx.ingress.kubernetes.io/proxy-body-size: 50m
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
tls:
- hosts:
- xx.somedomain.com
secretName: cert-key
rules:
- host: xx.somedomain.com
http:
paths:
- path: /blob/api/v1/some-backend
backend:
serviceName: blob-service
servicePort: 8080
when i test with api tester like talend is got 405 error
Try this
apiVersion: networking.k8s.io/v1
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(/|$)(.*)
For example, the ingress definition above will result in the following rewrites:
rewrite.bar.com/something rewrites to rewrite.bar.com/
rewrite.bar.com/something/ rewrites to rewrite.bar.com/
rewrite.bar.com/something/new rewrites to rewrite.bar.com/new
Refer : https://kubernetes.github.io/ingress-nginx/examples/rewrite/
Try adding annotation like this
nginx.ingress.kubernetes.io/rewrite-target: "/$1"
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 facing a problem in an ingress controller nginx kubernetes that is deployed in minikube :
when i included the route that nginx will use to redirect the request it didn't work , however when i remove the route it work :
So , as is explained in yaml file when i use /category it didn't work , however with with just / it is working
---- load balancer ---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: spare-ingress-dev
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: cluster-test-ip
servicePort: 80
- path: /category/
backend:
serviceName: cluster-category-ip
servicePort: 5200
----service-----
apiVersion: v1
kind: Service
metadata:
name: cluster-category-ip
spec:
selector:
app: category
ports:
- port: 5200
targetPort: 5200
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: spare-ingress-dev
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/app-root: /
spec:
rules:
- host: cluster.io
http:
paths:
- path: /api/v1.0/auth/(.+)
backend:
serviceName: cluster-auth-ip
servicePort: 6000
- path: /api/v1.0/category/(.+)
backend:
serviceName: cluster-category-ip
servicePort: 5200
- path: /category/(.+)
backend:
serviceName: cluster-category-ip
servicePort: 5200
i found the solution
i removed nginx.ingress.kubernetes.io/rewrite-target: $1
and i added
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/app-root: /
Can you try this -
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: spare-ingress-dev
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: $1
spec:
rules:
- http:
paths:
- path: /(.*)
backend:
serviceName: cluster-test-ip
servicePort: 80
- path: /category/(.*)
backend:
serviceName: cluster-category-ip
servicePort: 5200