Search code examples
apacheapache2mod-proxyhttp-proxy

Reverse proxy: unwanted URL change


I have a web-service that I would like to expose through the URL foo.com/bar. However my Apache reverse proxy does not work as intended. I have created the file 001-default.conf, which contains the following code:

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyRequests Off
    ProxyPass /bar http://foo.com:8080/bar
    ProxyPassReverse /bar foo.com:8080/bar

    DocumentRoot /var/www/foo/
    ServerName info.foo.com
    <Directory /var/www/foo>
            Options Indexes FollowSymLinks MultiViews Includes
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>
</VirtualHost>

Right now I receive a 404 error code.

I want to achieve all hits on foo.com/bar to be silently redirected to foo.com:8080/bar, meaning that the user should only see the URL foo.com/bar. The reverse proxy redirect should also include requests such as foo.com/bar?=foobar.

I did enable proxy_http and proxy:

% sudo a2enmod proxy_http        
  Considering dependency proxy for proxy_http:
  Module proxy already enabled
  Module proxy_http already enabled

I hope that there is someone out there that are able to help me with this.

Similar problem that did not solve my problem: apache reverse proxy changes url Transparent redirect to port 8080


Solution

  • Try and edit the config to:

    <VirtualHost *:80>
        <Proxy *> 
                Order deny,allow
                Allow from all
        </Proxy>
        ProxyPreserveHost On
        ProxyRequests Off
        ProxyPass /bar http://foo.com:8080  # Note removed /bar
        ProxyPassReverse /bar foo.com:8080  # Note removed /bar
    
        DocumentRoot /var/www/foo/
        ServerName info.foo.com
        <Directory /var/www/foo>
                Options Indexes FollowSymLinks MultiViews Includes
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
    </VirtualHost>
    

    You can also try with mod_alias to avoid te /bar/bar issue you might be facing now. I do not know much about it, see the documentation: http://httpd.apache.org/docs/2.2/mod/mod_alias.html

    Try something like adding this to your conf

     Alias /bar http://foo.com:8080
    

    Using this you might wan't to remove the proxy.