Apache proxy for multiple servers: comparing two solutions

apache-2.2PROXYrewritevirtualhost

I need to setup Apache proxies for multiple servers, one proxy for one server. I can do this in two ways:

  1. Setup one virtual host and multiple proxies. Each server is served as a sub-directory of the virtual host: aaa.bbb.ccc/test1 and aaa.bbb.ccc/test2

    <VirtualHost *:443>
        ServerName aaa.bbb.ccc
        RewriteEngine on
        RewriteRule ^/test1$ /test1/ [R]
        RewriteRule ^/test1(.*) http://localhost:8080$1 [P]          
        RewriteRule ^/test2$ /test2/ [R]
        RewriteRule ^/test2(.*) http://localhost:8081$1 [P]
        <Location "/test1">
            ...
        </Location>
        <Location "/test2">
            ...
        </Location>
        ....
    </VirtualHost>
    
  2. Setup N virtual hosts and N proxies, serving N servers. Each server will be served with one virtual host.

    NameVirtualHost *:443
    <VirtualHost *.443>
        ServerName aaa.bbb.ccc
        RewriteEngine on
        RewriteRule ^(.*) http://localhost:8080$1 [P]
        <Location "/">
            ...
        </Location>
    </VirtualHost>
    <VirtualHost *.443>
        ServerName ddd.bbb.ccc
        RewriteEngine on
        RewriteRule ^(.*) http://localhost:8081$1 [P]
        <Location "/">
            ...
        </Location>
    </VirtualHost>
    

Both works. However I don't know which one is better. Can someone compare the pros and cons of the two approaches?

Best Answer

There is generally really nothing wrong with either way. But the real big difference between the two is going to be certificate management. Option 1 only requires 1 certificate for SSL. Option 2 depends on what you are doing for hosts names. If the only thing that is changing is the sub-domain, you can use a wild card certificate or you will need a certificate for each virtual host. Both can become expensive.

Generally what will be the biggest real driver between the two options are going to be "business" type decisions. Making a cleaner separation between the internal servers.

How you are actually exposing the internal servers to the outside, is not the most effective way. Technically it is working, but you can come into a lot of issues. Links on the pages can render wrong. On the external website, you would actually be exposing internal links and give the appearance the site is broken. Images and Javascript will also not function correctly or do some funky stuff. You should really be using Proxy and ReverseProxy

ProxyRequests off
ProxyPass /test1/ http://127.0.0.1:8080/
ProxyPass /test2/ http://127.0.0.1:8081/
ProxyHTMLURLMap http://127.0.0.1:8080 /app1
ProxyHTMLURLMap http://127.0.0.1:8081 /app2

<Location /test1/>
        ProxyPassReverse /
        ProxyHTMLEnable On
        ProxyHTMLURLMap  /      /test1/
</Location>

<Location /test2/>
        ProxyPassReverse /
        ProxyHTMLEnable On
        ProxyHTMLURLMap /       /test2/
</Location>
Related Topic