Ingress nginx path routing - nginx

I have a React application deployed on AKS and the URL to the application landing page is https://myapp.host.com/dev/
Currently the application have multiple screens to navigate for example: /profile
When I click the profile navigation button, it redirects to this url: https://myapp.host.com/profile which is not what I want. Although the profile page is able to load, however if the page is refreshed it will return error as the application is not able to read the https://myapp.host.com/profile URL. By right, I want it to be https://myapp.host.com/dev/profile so that even if the page is refreshed, the app will able to load.
Here is my Ingress configuration:
ingress:
name: myapp-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/rewrite-target: /$1
hosts:
- host: myapp.host.com
paths:
- path: /dev/(.*)
pathType: Prefix
backend:
service:
name: myapp-svc
port:
number: 8080
tls:
- hosts:
- myapp.host.com
secretName: myapp-ingress-tls-secret
What should I change in my configuration?

I'm also a newbee for ingress, but maybe it's because that
according the ingress-nginx doc,
nginx.ingress.kubernetes.io/rewrite-target means that Target URI where the traffic must be redirected.
so
nginx.ingress.kubernetes.io/rewrite-target: /$1 means when you matched /dev/profile it will redirect to /profile
so maybe you can remove it and try again?

Related

Regex path matching for Ingress-Nginx

I am working with ingress-nginx in kubernetes to set up a server.
The issue is that the paths are not routing at all and I get a 404 error from the nginx server on any request I make.
Below is my code for ingress:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-srv
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
# defaultBackend:
# service:
# name: auth-srv
# port:
# number: 3000
rules:
- host: app.dev
- http:
paths:
- pathType: Prefix
path: /api/auth/?(.*)
backend:
service:
name: auth-srv
port:
number: 3000
- path: /api/coms/?(.*)
pathType: Prefix
backend:
service:
name: coms-srv
port:
number: 3000
If I uncomment the default backend service I get a response but as soon as I remove it I get the 404 nginx error. So I know its connecting to the services I set.
I don't know where I'm going wrong how to go about fixing this as I'm copying straight from the docs. Any help or insight would be great. Thank you in advance!
Edit 1: I removed the regex from the path and commented out the /api/auth path so no requests should be going to the auth-srv. For some reason, all requests route to the auth-srv even though there is no mapping to it. NOTE: Both the auth and coms pods/services are running in the background, just ingress-nginx still isn't routing properly.
So the reason why this wasn't routing properly was because of the:
- host: app.dev
- http:
The "-" in front of the "http" made the controller think it was its own ruleset so the following routes had a host of "*". After I Removed the "-" in front of the "http", the rules were set to the proper host of app.dev and it started routing accordingly.
Thank you for your help everyone! What a long day it has been :')

Nginx rewrite-target overwritting domain/suffix/index.php to domain/index.php

recently I have deployed an kubernetes cluster which is running wordpress instance and phpmyadmin. I'm using Nginx ingress controller to perform path based routing for both the services. However, request to / is happening without any hassle but when I request domain.com/phpmyadmin/ I get a login page after which I have been redirected to domain.com/index.php instead of domain.com/phpmyadmin/index.php. Please suggest me possible turn around for this. Thank you guys for the support :)
My ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-nginx
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/add-base-url : "true"
nginx.ingress.kubernetes.io/rewrite-target: "/$2"
# ingress.kubernetes.io/rewrite-target: "^/phpmyadmin/"
spec:
rules:
- host: example.domain.com
http:
paths:
- path: /
backend:
serviceName: wordpress
servicePort: 80
- path: /phpmyadmin(/|$)(.*)
backend:
serviceName: phpmyadmin
servicePort: 80
I'd say issue is not on Nginx Ingress side.
nginx.ingress.kubernetes.io/rewrite-target: "/$2"
...
- path: /phpmyadmin(/|$)(.*)
Should work properly for you.
However there is second part, configuration of myphpadmin. As you didn't provide this configuration I would guess what could cause this issue.
Like mentioned in phpmyadmin docs, sometimes you need to set $cfg['PmaAbsoluteUri']
In some setups (like separate SSL proxy or load balancer) you might have to set $cfg['PmaAbsoluteUri'] for correct redirection.
As I based on this configuration, many depends on how you configured PMA_ABSOLUTE_URI, is it http://somedomain.com/phpmyadmin or different?
Is important as you might encounter situation like:
When you enter to http://somedomain.com/phpmyadmin and login you will be redirected to http://somedomain.com/ so Ingress will redirect you to path: / set in ingress
If you will again enter http://somedomain.com/phpmyadmin you will be able to see phpmyadmin content, like you would be already logged in.
You could try to add env in your myphpadmin deployment. It would look similar like below:
env:
- name: PMA_ABSOLUTE_URI
value: http://somedomain.com/myphpadmin/
Last thing, its not recommended to use expose phpmyadmin without https.
For some extra information you can read this article.
In short:
Nginx ingress configuration looks ok
Check your myphpadmin configuration, especially PMA_ABSOLUTE_URI.

why redirect (server-side) to root on nginx ingress (reverse proxy) & rewrite-target

I'm using Kubernetes with Nginx ingress controller and I have two services (srv1 and srv2) on my namespace which I want to access them like this:
https://xyz.example.com/srv1/ => (point to the root of srv1)
https://xyz.example.com/srv2/ => (point to the root of srv2)
for example, if someone hit the below URL :
https://xyz.example.com/srv1/page1.aspx
then, they will hit the page1.aspx which is in the root of my application in srv1
it works just fine with the below ingress file:
Problem:
I have a second page which is page2.aspx and I placed it next to page1.aspx, so both of them are in root, now if I click on a button on the page1 which has a server-side code to redirect us to the page2.aspx, then I expected to get us into the below URL:
https://xyz.example.com/srv1/page2.aspx
but instead, it will redirect me to the below path:
https://xyz.example.com/page2.aspx
Just for clarification, I put an HTML link in the page1 which point to page2, it works just fine because it works in the client-side instead of the server-side.
so far I used the below 3 languages for the server-side redirection and all of them have the same behaviour:
C# & .net core (versions 2.1 , 2.2 , 3.0 and 3.1)
Python (version 3.7)
Ruby (version 2.7.1)
so I think the problem is from Kubernetes, anyone has any idea?
here are my ingress YAML:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ds1-ingress
namespace: ds1
annotations:
kubernetes.io/ingress.class: nginx
certmanager.k8s.io/cluster-issuer: letsencrypt-prod
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
tls:
- hosts:
- xyz.example.com
secretName: tls-secret
rules:
- host: xyz.example.com
http:
paths:
- backend:
serviceName: srv1
servicePort: 80
path: /srv1/(.*)
- backend:
serviceName: srv2
servicePort: 80
path: /srv2/*
Github's link for source-code:
https://github.com/POC-Vault/Routing
Docker-hub' link for anyone who likes to pull the image and test it.
https://hub.docker.com/repository/docker/farzadjalali/k8s-routing
How to pull the docker image:
docker push farzadjalali/k8s-routing:latest

Cannot access ingress subpath using nginx

I have a containerized application that I deployed to kubernetes, and I'm now working on a ingress route to add to my ingress controller so I can access the application from outside the cluster.
My application has an index root and this page contains hyperlinks for other pages. When creating the ingress route I can access the index page eg. /something, but then when I click on an hyperlink which would direct me to a new page /something/new, I get instead /new .
Accessing the pod directly I can navigate through the application just fine.
I tried changing the annotation and the regex code on the paths but nothing seems to work so far.
Here's my ingress:
metadata:
name: something-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- http:
paths:
- backend:
serviceName: svc-something
servicePort: 8000
path: /something(/|$)(.*)
Expected to access the path /something/new but get /new instead. Did anyone run into the same issue?

Nginx ingress resource - Redirect from to www (SSL doesn't work)

Use case
I deployed the nginx ingress controller in my Kubernetes cluster using this helm chart:
https://github.com/helm/charts/tree/master/stable/nginx-ingress
I created an ingress resource for my frontend serving webserver and it is supposed to redirect from non-www to the www version. I am using SSL as well.
The problem
When I visit the www version of my website everything is fine and nginx serves the page using my Lets Encrypt SSL certificate (which exists as secret in the right namespace). However when I visit the NON-www version of the website I get the failing SSL certificate page in my Browser (NET::ERR_CERT_AUTHORITY_INVALID) and one can see the page is served using the Kubernetes ingress fake certificate. I assume that's also the reason why the redirect to the www version does not work at all.
This is my ingress resource (actual hostnames have been redacted):
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
kubernetes.io/tls-acme: "true"
nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
creationTimestamp: 2018-10-03T19:34:41Z
generation: 3
labels:
app: nodejs
chart: nodejs-1.0.1
heritage: Tiller
release: example-frontend
name: example-frontend
namespace: microservices
resourceVersion: "5700380"
selfLink: /apis/extensions/v1beta1/namespaces/microservices/ingresses/example-frontend
uid: 5f6d6500-c743-11e8-8aaf-42010a8401fa
spec:
rules:
- host: www.example.io
http:
paths:
- backend:
serviceName: example-frontend
servicePort: http
path: /
tls:
- hosts:
- example.io
- www.example.io
secretName: example-frontend-tls
The question
Why doesn't nginx use the provided certificate on the non-www version as well?
Looks like you fixed the issue for receiving an invalid certificate by adding an additional rule.
The issue with the redirect looks like it's related to this and it's not fixed as of this writing. However, there is a workaround as described on the same link:
nginx.ingress.kubernetes.io/configuration-snippet: |
if ($host = 'foo.com' ) {
rewrite ^ https://www.foo.com$request_uri permanent;
}
I fixed it by adding the non www version to the rules. The redirect still doesn't work, but the page is served using the correct SSL certificate though.
- host: example.io
http:
paths:
- backend:
serviceName: example-frontend
servicePort: http
path: /

Resources