Two different domains and paths for same angular application

5/13/2019

I have setup an angular docker application (with nginx to serve it) in a Kubernetes cluster. In Kubernetes I have an ingress controller (the one from the kubernetes community) which shall route the traffic from the following domains:

a) https://app.somedomain.com and b) https://saas-customer.somedomain.com/app

to the same angular container. The idea is that individual users can use a) and company users/customers can use b). I have setup the following ingress for a):

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: frontend-ingress
  annotations:  
    kubernetes.io/ingress.class: nginx
spec:
  tls:
  - hosts:
    - app.somedomain.io
    secretName: tls-secret
  rules:
  - host: app.somedomain.io
    http:
      paths:
      - backend:
          serviceName: frontend-service
          servicePort: 80

and this for b)

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: frontend-ingress-wild
  annotations:  
    kubernetes.io/ingress.class: nginx
spec:
  tls:
  - hosts:
    - somedomain.io
    - '*.somedomain.io'
    secretName: tls-secret
  rules:
  - host: '*.somedomain.io'
    http:
      paths:
      - path: /app
        backend:
          serviceName: frontend-service
          servicePort: 80

a) is working as intended. With b) the problem is that the index.html is served, but it references assets from '/'. base href is set to / in angular.

src="polyfills.7037a817a5bb670ed2ca.js"

But i can not change the base href because in one case it should be '/' and in the other '/app' but still all assets should be served from one location. I do not know If i need to add specific route to angular for this to work or try to fiddle on the ingress/nginx side to solve this challenge.

-- Masiar Ighani
angular
kubernetes
nginx-ingress

1 Answer

5/13/2019

You want to serve your assets like *.js files from "/"? You can do it using following annotation in second ingress:

nginx.ingress.kubernetes.io/configuration-snippet: |
  rewrite ^/app/(.*\.js)$ /$1 break;

This way you app will be served from /app directory and your js files - from root like this:

/app/index.html -> /app/index.html
/app/index.js -> /index.js

Is this what you want to achieve?

-- Vasily Angapov
Source: StackOverflow