Accessing subdomains of webapp handled by nginx-ingress

4/13/2019

I have two services running:

$kubectl get services
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP          49m
shiny        NodePort    10.110.49.57   <none>        3838:30240/TCP   34m
web          NodePort    10.98.56.71    <none>        80:31758/TCP     39m

And the following ingress:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: / 
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /*
        backend:
          serviceName: web
          servicePort: 80
      - path: /shiny/*
        backend:
          serviceName: shiny
          servicePort: 3838

I get the behavior I want from shiny if I access it directly through minikube service shiny:

After applying the ingress, the subdomains stop working:

Why does this happen? I would like to access stuff in shiny, from web, through url's. Eg. <iframe src=.../shiny/test1></iframe>.

-- djfinnoy
kubernetes
nginx-ingress
shiny

2 Answers

4/15/2019

Nginx uses regex for matching the paths/locations of your requests. Try changing:

/shiny/* to /shiny/.*
-- Esteban Garcia
Source: StackOverflow

5/10/2019

The following yaml adjustments solved the problem:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /*
        backend:
          serviceName: web
          servicePort: 80
      - path: /shiny(/|$)(.*)
        backend:
          serviceName: shiny
          servicePort: 3838

Source: https://kubernetes.github.io/ingress-nginx/examples/rewrite/

Edit:

The above led to more trouble down the line. In the end, I went with the following ingress config:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      rewrite /$1 break;
      rewrite /shiny/(.*) /$1 break;
spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: web
              servicePort: 80
          - path: /shiny
            backend:
              serviceName: shiny
              servicePort: 3838
-- djfinnoy
Source: StackOverflow