Skip to content

Support regex for redirect action in VirtualServer and VirtualServerRoute #7789

Open
@AlxCloudRl

Description

@AlxCloudRl

We are trying to match a specific path in a VirtualServer, capture what's after that path, then redirect to another domain (301 redirect), appending the capture group to a specific part.

My initial URL would be something like https://mymedia.company.com/home/XXXXX, where XXXXX is a random string. I then want to redirect to https://myanotherwebsite.company.com/home/share/XXXXX, where XXXXX is the random string we extracted from the first domain.

This is what we would expect to be able to do:

Extract of the relevant part of the VirtualServer:

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  name: redirect-vs
spec:
  routes:
    - path: ~^/home/(.*)$
      action:
        redirect:
          url: https://myanotherwebsite.company.com/home/share/${1}
          code: 301

However, this is currently not supported.

  • ${1} is not accepted by NGINX: Invalid value: "1": '1' contains an invalid NGINX variable. Accepted variables are: scheme, http_x_forwarded_proto, request_uri, host
  • If I just put $1, without the closing brackets, it says that it's incorrect because a variable must be formatted like ${}.

With the 'redirect' action, only the following variables are supported: $scheme, $http_x_forwarded_proto, $request_uri, $host

Reaching out to the F5 support, the fix was to use http_snippets and server_snippets, which to be honest is confusing and not very user friendly when there is 10 ways to do things. The CRD could be improved for that.

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
name: redirect-vs
spec:
host: mymedia.company.com
http-snippets: |
map $request_uri $my_uri {
~^/home+/(.*)$ /home/share/$1;
default $uri;
}
server-snippets: |
location /home {
return 301 https://myanotherwebsite.company.com$my_uri;
}

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalAn issue that proposes a feature request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions