3

I have an angular 4 web application which is hosted on apache 2.4. The application makes use of an API written in nodejs javascript running over express. Both the website and the API service are running on the same machine but on different ports. The website is on port 80 and the API service is listening on port 9000.

I would like to set up apache to do reverse proxy for all the API calls.

For example, any url that contains /api/ I want it rewritten by apache to point to the API url:port. If I use ProxyPass like the following lines, the redirect works fine:

ProxyPass        "/api/V1/systeminfo" "http://localhost:9000/api/V1/systeminfo"
ProxyPassReverse "/api/V1/systeminfo" "http://localhost:9000/api/V1/systeminfo"

What I do not know how to do, is to use the ProxyPassMatch directive and create a regular expression so that any url that contains /api/ is redirected to http://localhost:9000/api/.....

I tried the following but it does not work:

ProxyPassMatch    "^/api.*$" "http://localhost:9000/$1"
ProxyPassReverse  "^/api.*$" "http://localhost:9000/$1"

Neither does the following:

ProxyPassMatch    "^/.*?/api.*?/v[0-9]+/(.*)$" "http://localhost:9000/$1"
ProxyPassReverse  "^/.*?/api.*?/v[0-9]+/(.*)$" "http://localhost:9000/$1"

Any help would be appreciated. My regex skills are lacking!

Note: obviously 'localhost' can be an IP address or a domain, I am using it in the example for simplicity.

Many thanks!

Edit: I corrected the first example to use .* instead of just * as per Alex's comment.

2
  • Two things I've noticed about the first regex. I think you mean ^/api.*$ not ^/api*$. Leaving out the period means you are looking for a repeated i. And the forward slash is not escaped with a backslash as is required by the regex flavors I've used. I would expect the backslash to also then require escaping but, I'm not familiar with the system you are dealing with. Sep 8, 2017 at 12:21
  • Thank you for this, I understand what you mean. The regex seems to be working now, but there is no redirect. Maybe I am doing something wrong with the second part of the ProxyPassMatch parameter. Not sure I understand what $1 is supposed to represent.
    – Dreamlord
    Sep 8, 2017 at 13:00

1 Answer 1

14

I solved the problem. The correct way to do reverse proxy with apache on the above example is the following:

ProxyPassMatch    "/api(.*)" "http://localhost:9000/api$1"
ProxyPassReverse  "/api(.*)" "http://localhost:9000/api$1"

I knew the multiple regex examples I was trying were correct, as I was testing them with https://regex101.com/, but I was hard coding the second part of to a particular route in order to eliminate the issue of the second part being incorrect, but for some reason it does not like that. Once I understood that the (.*) part of the regex is the first capture group and used it as $1 in the second part, it all worked.

I hope I clarified the answer enough and it is useful to someone else.

2
  • 1
    to continue with capture groups : consider the following snippet : ProxyPassMatch (.*)(\/websocket)$ ws://localhost:24423/$1$2 (.*) is the first capture group represented by $1 ((\/websocket)) is the second capture group represented by $2 Mar 27, 2019 at 16:57
  • good job, only thing that i removed "" quotes and that i had to make /$1 otherwise the apache didn't start
    – luky
    Apr 8, 2021 at 15:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.