How to I support both www.abc.com and abc.com?

November 25, 2019 126 views
Kubernetes

Hi, I recently set up my kubernetes with ingress. I am able to access my REST through www.abc.com/api for example, however, if I try to access it via abc.com/api or abc.com/ doesn’t seem to reach the server (404).

How can I enable my server to be reachable by both www.abc.com and abc.com?

Below is my ingress definition


apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: web-ingress
  annotations:
    kubernetes.io/ingress.class:  'nginx'
    nginx.ingress.kubernetes.io/from-to-www-redirect: "True"
spec:
  rules:
  - host: www.abc.com
    http:
      paths:
      - path: /
        backend:
          serviceName: abc-web-service
          servicePort: 80
      - path: /api
        backend:
          serviceName: abc-rest-service
          servicePort: 80
3 Answers

Hello, @brancojunio

The first step will be to make sure that both abc.com and www.adc.dom DNS is pointed to your droplet/server. If that is already configured your can simply redirect www to non-www or vice versa.

Let me know if you have any questions.

Regards,
Alex

Hi thank you for your reply!

I believe the domain is already set up to my droplet/server as I can access it via www.abc.com already. How would I go about redirecting any requests to “abc.com” to -> “www.abc.com” ?

Kind Regards
Junio

If you’re using Nginx, you could do something like the following within your Nginx config:

server {
  ....
  server_name example.com www.example.com;
  rewrite ^ https://www.example.com$request_uri? permanent;
  ....
}

Think different and code well,

-Conrad

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!