Apache Httpd - How to proxy-redirect two virtual hosts according to the path in requesting url?

1.2k Views Asked by At

I got a problem here, which could look trivial, but my basic knowledge in configuring apache and all the stuff I searched today didn't lead me to any satisfying solution.

I have two physical servers, and I run two applications on each one : one contained in a node.js instance, reachable and responding on port 4200 and another one contained in Tomcat on port 8080 (respectively my front-end and my back-end).

The requests from http client arrive on node server through port 80 and I configured an httpd to redirect them to the two instances according the path after the url.

The target behaviour is that httpd redirects every request to the node instance by default, and redirects to the remote tomcat the requests matching the following structure : physical_server_ip/api/*

So here's what I included in my httpd.conf on server hosting node instance and httpd :

    <VirtualHost *:80>
           #Filter on '/api/' path in url for node instance
           ProxyPass /api/ !
           ProxyPass / http://localhost:4200/
           ProxyPassReverse / http://localhost:4200/
           ProxyPreserveHost On
    </VirtualHost>

    <VirtualHost *:80>
            # Proxy to remote machine on which tomcat runs and provides    services
            ProxyPass /api/ http://<tomcat_machine_alias>:8080/xd-service/api/
            ProxyPassReverse /api/ http://<tomcat_machine_alias>:8080/xd-service/api/
            ProxyPreserveHost On
    </VirtualHost>

When i request my node machine through Apache, I can see the response from the first block (the node instance), but the others requests (those whose path begin with /api/) not.

In the browser console, it appears that these ones (the requests design to interrogate apis) are not proxied by apache and are sent to the node instance and not the tomcat.

What am I missing ?

1

There are 1 best solutions below

0
On BEST ANSWER

Dusan Bajic's answer resolved the problem : the two proxy rules sets needed to be merged in one virtual host, ordered from the most specific to the less specific.