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>
5
  • 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. Commented Oct 30, 2019 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
    Commented Oct 30, 2019 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. Commented Oct 30, 2019 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? Commented Oct 30, 2019 at 17:46
  • Exactly @StephenOstermiller. The old server will sit in the middle so to speak and should be invisible to connecting clients.
    – Dave
    Commented Oct 30, 2019 at 17:56

1 Answer 1

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>
5
  • 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
    Commented Oct 30, 2019 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
    Commented Oct 30, 2019 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. Commented Oct 30, 2019 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. Commented Oct 30, 2019 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
    Commented Oct 31, 2019 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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