skip to Main Content

I’m have a an Apache HTTP server that has a reverse proxy to a tomcat server. However, I only want the reverse proxy to happen when the client uses the subdomain www. This is because I want to use other subdomains to point to other applications, such as email.

e.g. www.example.com will go display the apache tomcat webapp.

The way to do this, I presume, is to configure my DNS so that every subdomain I use will point to my server. Right now, in addition to www, that is server.example.com and posfixadmin.example.com. However, the issue is that all my subdomains end up pointing to tomcat.

So when I try to visit postfixadmin.example.com/setup.php to set up postfixadmin through its web setup, it ends up taking me to my tomcat webapp’s 404.

Here is my virtualhost configuration:

<VirtualHost www.example.com:80>
    ServerName http://www.example.com
    ProxyPass / http://localhost:8080
    ProxyPassReverse / http://localhost:8080
</Virtualhost>

<VirtualHost server.example.com:80>
    ServerName server.example.com
    DocumentRoot /var/www/html/
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =server.example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>

<VirtualHost postfixadmin.example.com:80>
    ServerName postfixadmin.example.com
    DocumentRoot /var/www/postfixadmin/public

    ErrorLog /var/log/httpd/postfixadmin_error.log
    CustomLog /var/log/httpd/postfixadmin_access.log combined

    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>

    <Directory /var/www/postfixadmin/public>
       Options FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
    </Directory>
</VirtualHost>

EDIT
It looks like the proxy conf file doesn’t do anything (??). I decided to experiment around and change the first virtualhost servername to the following:

<VirtualHost *:80>
    ServerName abcd.example.com
    ProxyPass / http://localhost:8080
    ProxyPassReverse / http://localhost:8080
</Virtualhost>

Then, I restarted and reloaded Apache…But for some reason, going to www.example.com STILL took me to the tomcat webapp! Does anyone know what drives this?

2

Answers


  1. Chosen as BEST ANSWER

    I got it!

    It turns out that the problem was in the ssl configuration file - the :443 ports were overlapping.

    Thanks for the help!


  2. As to the DNS: I have set specific CNAME entries for each subdomain including www; all of them point back to the public IP of my server that houses my example.com domain (using @ in my case – possible with most DNS, I think). There may be some different strategies on this, but I believe you’re on the correct path based on what you’ve suggested in the question.

    As to Apache configuration:
    I believe that the http protocol does not need to be specified in the ServerName directive and that, generally, the domain need not appear inside the <VirtualHost></VirtualHost> tags.

    I should mention that I am relatively unfamiliar with Tomcat but am assuming it is listening at 8080 on the localhost, in which case this should help.

    I’m not 100% certain that that is all that is snarling you, but try trimming that ServerName back and doing like so, including the change to the VirtualHost open tag:

    <VirtualHost *:80>
        ServerName www.example.com
        ProxyPass / http://localhost:8080
        ProxyPassReverse / http://localhost:8080
    </Virtualhost>
    

    Your second <VirtualHost> probably requires similar changes, though it also seems that you are directing it to serve requests from the web/network which are coming in on port 8080 — which I don’t believe is your intent.

    I think what you want is to also listen on port 80 from the web/network, but to follow these directives if addressed to server.example.com like so:

    <VirtualHost *:80>
        ServerName server.example.com
        DocumentRoot /var/www/html/
        RewriteEngine on
        RewriteCond %{SERVER_NAME} =server.example.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
    </VirtualHost>
    

    And finally, similar change to the opening <VirtualHost> tag on the final one:

    <VirtualHost *:80>
        ServerName postfixadmin.example.com
        DocumentRoot /var/www/postfixadmin/public
    
        ErrorLog /var/log/httpd/postfixadmin_error.log
        CustomLog /var/log/httpd/postfixadmin_access.log combined
    
        <Directory />
            Options FollowSymLinks
            AllowOverride All
        </Directory>
    
        <Directory /var/www/postfixadmin/public>
           Options FollowSymLinks MultiViews
           AllowOverride All
           Order allow,deny
           allow from all
        </Directory>
    </VirtualHost>
    

    Altogether, this seems more like what you’re looking for:

    <VirtualHost *:80>
        ServerName www.example.com
        ProxyPass / http://localhost:8080
        ProxyPassReverse / http://localhost:8080
    </Virtualhost>
    
    <VirtualHost *:80>
        ServerName server.example.com
        DocumentRoot /var/www/html/
        RewriteEngine on
        RewriteCond %{SERVER_NAME} =server.example.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName postfixadmin.example.com
        DocumentRoot /var/www/postfixadmin/public
    
        ErrorLog /var/log/httpd/postfixadmin_error.log
        CustomLog /var/log/httpd/postfixadmin_access.log combined
    
        <Directory />
            Options FollowSymLinks
            AllowOverride All
        </Directory>
    
        <Directory /var/www/postfixadmin/public>
           Options FollowSymLinks MultiViews
           AllowOverride All
           Order allow,deny
           allow from all
        </Directory>
    </VirtualHost>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search