ingress not working over http default backend shows 404

Viewed 36

I have a problem with Kubernetes deployment. My service works perfectly when accessing "example.com" or "https://example.com" but it does not work when accessing over "www.example.com" or "http://example.com" the default nginx backend shows up with a 404.

Here is my ingress implementation:

# Ingress
apiVersion: networking.k8s.io/v1
# make a new cert
kind: Ingress
metadata:
  name: ${APP_NAME}
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: 'true'
    nginx.ingress.kubernetes.io/rewrite-target:  /
spec:
  tls:
  - secretName: ${APP_NAME}
    hosts:
    - ${URL}
    - www.${URL}
  rules:
  - host: ${URL}
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: ${APP_NAME}
            port:
              number: 80

here is my certificate it should be fine with both domains:

# Certificate
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: ${APP_NAME}
spec:
  secretName: ${APP_NAME}
  dnsNames:
  - example.com
  - www.example.com
  issuerRef:
    name: letsencrypt-production
    # We can reference ClusterIssuers by changing the kind here.
    # The default value is Issuer (i.e. a locally namespaced Issuer)
    kind: ClusterIssuer
    group: cert-manager.io
1 Answers
  1. Add a DNS record in your DNS provider space ( a CNAME type) which maps "www" to "example.com"

  2. Make sure your SSL certificate has the domain "www.example.com" included,

Example here, for an SSL generated by LetsEncrypt with DNS zone hosted in Azure:

apiVersion: cert-manager.io/v1alpha2
kind: Certificate
...
  commonName: example.com
  dnsNames: #list of all different domains associeted with the certificate
    - example.com
    - www.example.com
  acme:
    config:
      - dns01:
          provider: azure-dns
        domains:
          - example.com
          - www.example.com
...
  1. In your Ingress file, you can remove the hosts www.${URL}
Related