0

I am trying to forward specific uri if matched to backend in nginx. For example

forward https://www.hostname.com/*/a/b/c to https://int.hostname.com/*/a/b/c (Where * is a variable auto populated from regex)

Current configuration looks like below and have no idea how to proceed on above

location /a/b/c/ {
    proxy_set_header        Host $proxy_host;
    proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_read_timeout      180m;

    proxy_pass http://int.hostname.com/v2/e/t/a/b/c;
    proxy_redirect default;
}
2
  • To clarify, in the case where the uri matches "/*/a/b/c" are you sending to the same path on the internal server, or is there some modification? e.g. if someone hits /v1/a/b/c are you just talking to /v1/a/b/c on internal? or is the "/v1" part somehow important Commented Jul 27, 2017 at 5:40
  • @JoshuaDeWald It will be same. /v1/a/b/c will be pass to backed as /v1/a/b/c. Similarly, /sample/v2/a/b/c will be passed as /sample/v2/a/b/c. * means any path. Thanks Commented Jul 27, 2017 at 5:47

1 Answer 1

1

If you want your location to match any URI that ends with /a/b/c/, you will need to use regular expression syntax. See this document for details.

The URI suffix of the proxy_pass statement is optional. In the absence of a URI suffix, the original URI will be passed unmodified. See this document for details.

For example:

location ~ /a/b/c/$ {
    ...
    proxy_pass http://int.hostname.com;
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks I will take a look at those docs. You mean I do need to write anything in proxy_pass? Actually, URI may not end with /a/b/c/. Considering this, what should be done if it doesn't end with it?
You mean that /a/b/c/ is embedded anywhere inside the URI? In which case, remove the terminating $.
yeah, that's the case and it should also forward exactly same to backend. If URI doesn't content /a/b/c/ then it shouldn't forward URI to backend. I hope this works. If there is anything else that I should take care of please let me know. I will test it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.