default backend - 404 without www ( ingress )

Viewed 3084
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 ?

3 Answers

add an ingress alias annotation to the annotations block:

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

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"

Not sure our solution applies in you case. We also faced similar issue(not exact), we didn't want to change the deployed ingress(i.e. can't add server-alias) So we some how made sure that all host address typed in browser/request translates to what is configured in ingress.

In this example, we would have created entry in local /etc/hosts

www.domain.com

Related