3

I have a single external address that points to a server that is serving multiple domains using virtual hosts. I would like to serve one of the virtual hosts on a new physical machine and am trying to use a reverse proxy to make this happen.

On the main server the configuration looks like this (names changed to protect the guilty):

<VirtualHost *:80>
    DocumentRoot /abc/htdocs/virtual/swnc
    ServerName swnc.org
    <Directory "/abc/htdocs/virtual/swnc">
        Options FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

On the new physical machine I have the domain referenced above configured with:

<VirtualHost *:80>
    ServerName swnc.org
    ProxyPass "/" "http://www.example.org/virtual/swnc"
    ProxyPassReverse "/" "http://www.example.org/virtual/swnc"
</VirtualHost>

This configuration works but the resulting URL is what is specified in the ProxyPass statement above rather than the "real" URL which should be swnc.org.

I tried adding ProxyPreserveHost On in the configuration but it did not make any difference. Is there something I am missing or is it simply not possible to proxy the request to the new machine and retain the "real" domain name?

Update:

Here are the Apache configuration files on the old server and the new server using the suggestions from Stephen's answer.

Old Server

<VirtualHost *:80>
    ServerName ncdrought.org
    ProxyPass "/" "http://new.ncdrought.org"
    ProxyPassReverse "/" "http://new.ncdrought.org"
</VirtualHost>

New Server

<VirtualHost *:80>
    ServerName   new.ncdrought.org
    DocumentRoot /var/www/ncdrought.org
    <Directory "/var/www/ncdrought.org">
    Options FollowSymLinks
    AllowOverride All
    Require all granted
    </Directory>
</VirtualHost>
  • Can you explain more about why you want a reverse proxy? Normally when you want to move a domain to a new server, you would point the DNS A records to the new server and not have to have any proxies. – Stephen Ostermiller Oct 30 '19 at 17:02
  • Glad you asked. Both servers sit behind an F5 proxy already and the SSL certificate being used (served by the F5) has both domains associated with it but only a single address visible to the outside world. For reasons that I have not been able to get a good answer to I've been told that since there is a single external address "they" are unable to point one of the domains to a new internal address. Doesn't sound correct to me so I'm trying this approach while attempting to get the more logical, cleaner and clearer solution of redirecting at the F5 done. – Dave Oct 30 '19 at 17:41
  • That makes some sense. You might have to get two new SSL certificates to be able to point the F5 to different machines for the different hosts. – Stephen Ostermiller Oct 30 '19 at 17:45
  • So you want traffic to come into the old server, just like it always had. But then you want to forward those requests to the new server, correct? – Stephen Ostermiller Oct 30 '19 at 17:46
  • Exactly @StephenOstermiller. The old server will sit in the middle so to speak and should be invisible to connecting clients. – Dave Oct 30 '19 at 17:56
3

The proxy directives have to go on the the old machine. The machine that is acting like the pass-through needs to be configured as the proxy.

On your new machine, you need to set it up with a different host name, like a sub-domain, .local domain, or IP address that can be accessed on your internal network.

So the configuration should like like this:

old machine

<VirtualHost *:80>
    ServerName swnc.org
    ProxyPass "/" "http://swnc-backend.local/"
    ProxyPassReverse "/" "http://swnc-backend.local/"
</VirtualHost>

new machine

<VirtualHost *:80>
    DocumentRoot /abc/htdocs/virtual/swnc
    ServerName swnc-backend.local
    <Directory "/abc/htdocs/virtual/swnc">
        Options FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
  • Thanks Stephen. That makes perfect sense. Looks like I was close but not quite there. It will take me a little while to give this a try and I'll be back to accept your answer when it works :) – Dave Oct 30 '19 at 18:35
  • That got me a LOT closer. Things are now getting to the new server but resources are not being loaded correctly. I modified the original question with copies of the new and old server configuration files with no names changed at all. My own machine is the "old" server in this case and I have the new server IP in my hosts file with the new name to simulate what would be done on the actual server. As I said, the new site contents are being delivered but the resources for it are not. The resources are being looked for using the old server name not the new one. – Dave Oct 30 '19 at 20:09
  • How are your resources linked? I always recommend using "root relative" links for resources. That is URLs that start with a slash. Like <img src=/images/foo.jpg">. If you do that, your resources should load internally as http://new.example/images/foo.jpg or externally as http://example.com/images/foo.jpg. – Stephen Ostermiller Oct 30 '19 at 21:48
  • 2
    If you really need to you can get the proxy to rewrite the links to your resources using an additional module: httpd.apache.org/docs/2.4/mod/mod_proxy_html.html However, I recommend not doing so. It is almost always just as easy to set up your links as root relative. Rewriting the HTML is not always good for performance either. – Stephen Ostermiller Oct 30 '19 at 21:50
  • I'm not sure what happened but when I tested things again this morning they are working correctly. Thank you again Stephen for your precise answer which is working just fine. Now to put it into production. – Dave Oct 31 '19 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.