Apache mod_rewrite as proxy not working

2.7k views Asked by At

I'm trying to use apache as a proxy server to redirect some requests to a service running on a different port. I'm attempting to follow the instructions here: http://httpd.apache.org/docs/2.4/rewrite/proxy.html

I've done exactly this to install (and nothing else):

sudo apt-get install apache2
sudo a2enmod rewrite
sudo a2enmod proxy
sudo a2enmod proxy_http

Then edited /etc/apache2/httpd.conf to be:

RewriteEngine on
RewriteRule ^/(.*) http://localhost:9200/$1 [P]
ProxyPassReverse / http://localhost:9200/

Then finally, restarted apache2 (sudo service apache2 restart). Eventually, I'll add some real conditions to that rule, but I'm just trying to test it for now.

But it's not working. curl -XGET localhost:80/foo fails with a 404 response, curl -XGET loaclhost:9200/foo succeeds. Nothing useful in the error log or access log.

1

There are 1 answers

2
Giacomo1968 On

I would recommend setting your Apache reverse proxy commands to be something like this; note the ProxyPass setting as well as ProxyRequests and ProxyPreserveHost settings:

 <IfModule mod_proxy.c>

  # Proxy specific settings
  ProxyRequests Off
  ProxyPreserveHost On

  <Proxy *>
    AddDefaultCharset off
    Order deny,allow
    Allow from all
  </Proxy>

  ProxyPass / http://localhost:9200/ 
  ProxyPassReverse / http://localhost:9200/ 

</IfModule>

I would also recommend you check the headers of the response output in curl by using the -I option like this:

curl -I localhost:80/foo

It should show you the exact response from the server so you can fully understand what might be failing and what might be working.