Tuesday, December 16, 2014

Apache: Reverse proxy the whole domain



I asked the same question a while ago, but I guess I didn't put my question right. I'm trying to reverse proxy one whole virtual host domain to a subdirectory of another virtual host, something like this http://host2.com -> http://host1.com/host2.



Apache's default site file is this





ServerAdmin webmaster@localhost
ServerName "host1.com"


Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order deny,allow
Allow from all



DocumentRoot /srv/www/host1
WSGIScriptAlias / /srv/www/host1/apache/django.wsgi





ServerAdmin webmaster@localhost

ServerName "host2.com"
ProxyRequests Off


Order deny,allow
Allow from all


ProxyPass / http://host1.com/host2
ProxyPassReverse / http://host1.com/host2





At this moment, the problem is that whenever I go to http://host2.com it shows me http://host1.com instead of http://host1.com/host2. What am I missing? I'm not sure if it should matter, but host1 is hosted using Django with wsgi.


Answer



ProxyPass is very nit-picky about slashes; since you're proxying a trailing slash (just the root, /), you'll want to proxy TO a trailing slash.



So, adding some trailing slashes to your targets, as below, should help out.




Also, since the device you're proxying too is the local system, you may want to avoid potential name resolution/NAT confusion by using 127.0.0.1. What you place here has no bearing on the host header received by the server, and the client header is passed through, as long as you've got ProxyPreserveHost On set:



ProxyPreserveHost On
ProxyPass / http://127.0.0.1/host2/
ProxyPassReverse / http://127.0.0.1/host2/


If that doesn't work, prehaps have host2 serve up some temporary content page to verify that you're hitting the correct vhost.



Side note, you don't want that block. It's built for when you're running in ProxyRequests on mode, and doesn't work as desired for reverse proxies (use instead for access control)



No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...