skip to Main Content

I have two domain names, each for different applications hosted in a single kubernetes cluster.

Is there a way to configure ingress to redirect to the different apps based on the hostname in the request it receives?

For example:

www.app1.com and www.app2.com point to the same IP address. However, I want www.app1.com to redirect to /appABC while www.app2.com redirect to /appXYZ.

I have attempted to capture the host name and use this to determine the redirect but it doesn’t work.

Is what I’m trying to do possible with NGINX?

2

Answers


  1. Chosen as BEST ANSWER

    After some experimentation, using the NGINX Playground, I was able to come up with this solution.

    ...
    nginx.ingress.kubernetes.io/server-snippet: |
        set $is_app1_base 1;
        set $is_app2_base 1;
        if ($host !~ "^.*app1.com$" ) {
            set $is_app1_base 0;
        }
        if ($request_uri != "/") {
            set $is_app1_base 0;
            set $is_app2_base 0;
        }
        if ($is_app1_base = 1) {
          return 301 $scheme://$host/appABC;
        }
        if ($host !~ "^.*app2.com$" ) {
          set $is_app2_base 0;
        }
        if ($is_app2_base = 1) {
          return 301 $scheme://$host/appXYZ;
        }
    

    In case you're wondering why a number of if statements had to be used this way, NGINX is not that great with if statements and logical operations.

    Another caveat worth stating here is that all ingresses associated with this NGINX controller will be affected by this server-snippet; Because nginx.ingress.kubernetes.io/server-snippet is a global annotation.


  2. Yes,it is Possible. You must need to create two configuration files and point them to their respective paths. Please follow this link for more info and refer to this SO also to get further idea on how to use.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search