Kubernetes Ingress www.example.com gives 404 while https://example.com works

Viewed 45

Does anyone know what could be the problem that i'm getting a 404 when trying to access the website via www.example.com while https://example.com works without any issues.

Here is the example of my ingress:

# Ingress
apiVersion: networking.k8s.io/v1
# make a new cert
kind: Ingress
metadata:
  name: ${APP_NAME}
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: 'true'
    nginx.ingress.kubernetes.io/from-to-www-redirect: 'true'
spec:
  defaultBackend:
    service:
      name: ${APP_NAME}
      port:
        number: 80
  tls:
  - secretName: ${APP_NAME}
    hosts:
    - ${URL}
    - www.${URL}

Also I tried to run

kubectl describe ingress 

it returns:

host: example.com

Is there an issue with the configuration or why does the www. not redirect properly?

1 Answers

You don't specify the hosts. They need to be specified in spec.rules, for example:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: name-virtual-host-ingress-no-third-host
spec:
  rules:
  - host: first.bar.com
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: service1
            port:
              number: 80
  - host: second.bar.com
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: service2
            port:
              number: 80

Note: Keep in mind that TLS will not work on the default rule because the certificates would have to be issued for all the possible sub-domains. Therefore, hosts in the tls section need to explicitly match the host in the rules section.

Source

Related