skip to Main Content

I am running Nginx ingress inside one of our EKS cluster and earlier had issue to http–>https redirect as we are terminating SSL at AWS NLB. I was able to get it fixed using the method listed in this ticket https://github.com/kubernetes/ingress-nginx/issues/2724 ( Thanks to @Ariseaz )

However, along with https redirect we want to append path and redirect which is not working. Here are some methods I have tried so far

The container webpage serving paths /coffee and /tea

http://cafe.com ——> https://cafe.com ## This works because of the http –> https redirection
http://cafe.com/tea —–> https://cafe.com/tea ## This works
http://cafe.com/coffee —–> https://cafe.com/coffee ## This works

Now when I want to redirect https://cafe.com to https://cafe/coffee it does not work.

Can anyone please tell me now to append path to https:hostname and redirect.. I was able-to get is working with AWS ALB Ingress with this annotation and I am trying to get the same method with nginx ingress.

alb.ingress.kubernetes.io/actions.svc-cafe: >
      {"Type":"redirect","RedirectConfig":{"Path":"/coffee","Protocol":"HTTPS", "Port": "443","StatusCode":"HTTP_301"}} ## This is to append /coffee to hostname and redirect ( https://cafe.com ---> htttps://cafe.com/coffee)

Here is my ingress file using Nginx ingress controller

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: cafe-example
  annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
      if ($host = "https://cafe.com") {
          return 301 https://$host$request_uri/coffee;
        }     
    #nginx.ingress.kubernetes.io/rewrite-target: /
    #nginx.ingress.kubernetes.io/configuration-snippet: |
    #    rewrite ^(/coffee)$ $1/ permanent;    
        
spec:
  ingressClassName: internal-nginx
  rules:
  - host: cafe.com
    http:
      paths:
      - path: /tea
        pathType: Prefix
        backend:
          service:
            name: tea-svc
            port:
              number: 80
      - path: /coffee
        pathType: Prefix
        backend:
          service:
            name: coffee-svc
            port:
              number: 80

3

Answers


  1. Chosen as BEST ANSWER

    When I change the host to cafe.com instead of https://cafe.com I can see it the url getting redirected but its going in a loop

    nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($host = 'cafe.com' ) {
         return 301 https://$host$request_uri/coffee;
      } 
    
    https://cafe.com//coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee/coffee
    

  2. Please try :

    nginx.ingress.kubernetes.io/server-snippet: |
            if ($host ~ "https://cafe.com")
            {
                rewrite ^ https://$host$request_uri/coffee permanent;
            }
    

    or else try

    nginx.ingress.kubernetes.io/configuration-snippet: |
          if ($host = 'https://cafe.com' ) {
             return 301 https://$host$request_uri/coffee;
          }
    
    Login or Signup to reply.
  3. You can try this

    nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($host = 'cafe.com' ) {
         return 301 https://cafe.com/coffee;
      } 
    

    OR

    nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($host = 'cafe.com' ) {
         rewrite ^([^.]*[^/])$ https://cafe.com/coffee permanent;
      } 
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search