Apache2 parallel reverse Proxy and Directory directives

apache-2.4reverse-proxy

I'm trying to set up a server which processes "normal" connections to a directory on the server, but will redirect any calls to example.com/api to another service, through reverse proxy (no rewrite possible there).

So far, my efforts only resulted in either landing on the index page of the website, or getting a 500 status code incriminating the server, definitely not the target service…

Here is the Apache configuration:

<VirtualHost *:80>
        ServerAdmin me@example.com
        ServerName example.com
        UseCanonicalName Off
        #DocumentRoot /var/www/website/public
        Redirect permanent / https://website.com/

</VirtualHost>
<VirtualHost *:443>
        ServerAdmin me@example.com
        ServerName example.com
        UseCanonicalName Off
        ProxyRequests On

        <Proxy https://example.com/api>
                Order deny,allow
                Allow from all
                ProxyPreserveHost On
                ProxyPass http://proxy:port
                ProxyPassReverse http://proxy:port
        </Proxy>
        DocumentRoot /var/www/website/public
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/website/public>
                Require all granted
                AllowOverride All
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        [SSL here]
</VirtualHost>

Is that even possible? If yes, what am I doing wrong?

Best Answer

Your syntax here is a bit odd:

...
    <Proxy https://website.com/api>
            Order deny,allow
            Allow from all
            ProxyPreserveHost On
            ProxyPass http://proxy:port
            ProxyPassReverse http://proxy:port
    </Proxy>
...

As far as I can tell you would replace that by a either a simple ProxyPass directive:

ProxyPass /api/ http://backend.example.com:port/
ProxyPassReverse /api/ http://backend.example.com:port/

Or if you prefer you can enclose the ProxyPass directive in a Location block

<Location /api/>
   ProxyPass  http://backend.example.com:port/
   ProxyPassReverse  http://backend.example.com:port/
</Location> 

You currently have ProxyPreserveHost on which is something you rarely need, so I have omitted that from my examples.

Related Topic