Search code examples
regexnginxurl-rewriting

Rewrite Path of Url Regex Nginx


I am trying to write a url rewrite for the following case. I have a proxy server which has sub routes and each route is mapped to a backend. I need a regex for the following case

/proxy/vms/some/cool/route ==> /some/cool/route
/proxy/oms/another/route ===> /another/route
/proxy/ims/another/route?query ===> /another/route?query
/proxy/some-word-with-character/fourth/route ===> /fourth/route

Basically the /proxy part and the immediate child is stripped off from the url. The immediate child can have special characters

I tried the following regex

'^/proxy(/.*)$': '$1'

But im getting incorrect groups in this . Any help would be useful


Solution

  • You can take the part after the second / and then match 1 or more non whitespace characters.

    Then use that group 1 value.

    ^/proxy/[^/]*(/\S+)
    

    Regex demo