default backend - 404 without www ( ingress )

10/28/2019
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 annotations:
  nginx.ingress.kubernetes.io/rewrite-target: /
  ingress.bluemix.net/rewrite-path: "serviceName=nginx rewrite=/"
 name: nginx-ingress 
 namespace: 'default'  
spec:
 rules:
 - host: www.domain.com
   http:
     paths:
     - path: /*
       backend:
         serviceName: nginx
         servicePort: 80

Here I have some ingress config yaml file. When I apply it all working correctly but only when you go by path www.domain.com , when I try to use domain.com , it doesn't work and return me

default backend - 404

What should I do ? Add one more host to the rules:

 - host: domain.com
   http:
     paths:
     - path: /*
       backend:
         serviceName: nginx
         servicePort: 80

Like this or I can use better solving of this problem ?

-- Andrey Radkevich
google-kubernetes-engine
kubernetes
nginx-ingress

2 Answers

10/28/2019

add an ingress alias annotation to the annotations block:

nginx.ingress.kubernetes.io/server-alias:  domain.com
-- Efrat Levitan
Source: StackOverflow

10/28/2019

You can set another host rule for the domain.com. However, this is more suitable if you want different path rules for it. To use the same rules, it's better to set the server-alias annotation:

Allows the definition of one or more aliases in the server definition of the NGINX configuration using the annotation nginx.ingress.kubernetes.io/server-alias: "<alias 1>,<alias 2>". This will create a server with the same configuration, but adding new values to the server_name directive.

Use this:

nginx.ingress.kubernetes.io/server-alias: domain.com

Remember that you can configure the from-to-www-redirect annotation too:

In some scenarios is required to redirect from www.domain.com to domain.com or vice versa. To enable this feature use the annotation nginx.ingress.kubernetes.io/from-to-www-redirect: "true"

-- Eduardo Baitello
Source: StackOverflow