1

I have installed multiple tomcat instances in Ubuntu. I want to specify different URL to each tomcat instance. But I am not able to achieve desired result in case of multiple tomcat instances.

(Below URL is just to explain what I am trying to do)

Tomcat1: demo.mydomain.com/myapp: localhost:8080/myapp

Tomcat2: test.mydomain.com/myotherapp: localhost:8081/myotherapp

Modifying my Config Files after further search: I am trying to achieve it via AJP Port. So I did below Editing in my files:

Tomcat1 = demo

Tomcat2 = test

demo - server.xml

 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

test- server.xml

 <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />

workers.properties

worker.list=jk-status,demo,test
# Status worker for managing load balancer
worker.jk-status.type=status # setting type of jk-status' worker.

worker.demo.port=8009
worker.demo.host=localhost
worker.demo.type=ajp13

worker.test.port=8010
worker.test.host=localhost
worker.test.type=ajp13

Site enabled in /etc/apache2/sites-enabled

1_rewritehttp.conf

<VirtualHost *:80>
    ServerName server
    RewriteEngine On
    RewriteRule (.*) http://server.mydomain.com%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:80>
        ServerName server.mydomain.com
        ServerAdmin webmaster@localhost
        DocumentRoot /opt/www/

        JkMount /jolokia-demo/* demo
        JkMount /jolokia-test/* test

        <Directory /opt/www/>
                Options Indexes FollowSymLinks
                AllowOverride None
                Require all granted
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerName demo
    RewriteEngine On
    RewriteRule (.*) https://demo.mydomain.com%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:80>
    ServerName demo.mydomain.com
    RewriteEngine On
    RewriteRule (.*) https://demo.mydomain.com%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:80>
    ServerName test
    RewriteEngine On
    RewriteRule (.*) https://test.mydomain.com%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:80>
    ServerName test.mydomain.com
    RewriteEngine On
    RewriteRule (.*) https://test.mydomain.com%{REQUEST_URI}
</VirtualHost>

3_demo.conf

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerAdmin webmaster@localhost
    ServerName server.mydomain.com

        DocumentRoot /opt/www/
        <Directory /opt/www/>
            Options Indexes FollowSymLinks
            AllowOverride None
            Require all granted
        </Directory>


        JkMount /myapp/* demo

        Alias /myapp/ "/opt/tomcat/demo/webapps/myapp/"
        <Directory /opt/tomcat/demo/webapps/myapp/>
            Options Indexes FollowSymLinks
            AllowOverride None
            Require all granted
        </Directory>


        JkMount /manager/* demo

        ErrorLog ${APACHE_LOG_DIR}/demo_error.log
        CustomLog ${APACHE_LOG_DIR}/demo_access.log combined

        SSLEngine on

        SSLCertificateFile /usr/local/ssl/crt/public.cer
        SSLCertificateKeyFile /usr/local/ssl/private/private.key
        SSLCertificateChainFile /usr/local/ssl/crt/intermediate.cer

        BrowserMatch "MSIE [2-6]" \
                nokeepalive ssl-unclean-shutdown \
                downgrade-1.0 force-response-1.0
        # MSIE 7 and newer should be able to use keepalive
        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
    </VirtualHost>
</IfModule>

4_test.conf

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerAdmin webmaster@localhost
    ServerName server.mydomain.com

        DocumentRoot /opt/www/
        <Directory /opt/www/>
            Options Indexes FollowSymLinks
            AllowOverride None
            Require all granted
        </Directory>


        JkMount /myotherapp/* test

        Alias /myotherapp/ "/opt/tomcat/test/webapps/myotherapp/"
        <Directory /opt/tomcat/test/webapps/myotherapp/>
            Options Indexes FollowSymLinks
            AllowOverride None
            Require all granted
        </Directory>


        JkMount /manager/* test

        ErrorLog ${APACHE_LOG_DIR}/test_error.log
        CustomLog ${APACHE_LOG_DIR}/test_access.log combined

        SSLEngine on

        SSLCertificateFile /usr/local/ssl/crt/public.cer
        SSLCertificateKeyFile /usr/local/ssl/private/private.key
        SSLCertificateChainFile /usr/local/ssl/crt/intermediate.cer

        BrowserMatch "MSIE [2-6]" \
                nokeepalive ssl-unclean-shutdown \
                downgrade-1.0 force-response-1.0
        # MSIE 7 and newer should be able to use keepalive
        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
    </VirtualHost>
</IfModule>


Now, The problem is I am able to access demo tomcat through URL. If I try to access test tomcat URL, it redirects to demo tomcat URL. I Tried accessing manager of both the tomcats and they are both pointing to demo tomcat manager, if I do below command then it redirects to test tomcat manager. I want to distinguish between both tomcat instances. I am missing something here. Any help would be appreciated.

a2dissite 3_demo.conf

Not able to find any proper way anywhere, So asked here. Please feel free to suggest- How to do it from scratch. Thanks in advance.

1 Answers1

2

Your server config contains overlapping virtual hosts. All your hosts are named Demo.Server and all of them use the same IP/port definition. This way, only the first one is recognized, the rest are not.

Also, your question includes Tomcat servers listening on ports 8080 and 8081, which suggest HTTP, yet you have Jk configured, which is used for AJP.

Assuming you use HTTP, you can achieve your goal with the following Apache config (you don't need mod_jk in this case):

<VirtualHost *:443>
    ServerName www.tomcat1.com

    ProxyPass / http://localhost:8080/
    SSLCertificateFile /certfiicate/for/server1.pem
</VirtualHost>

<VirtualHost *:443>
    ServerName www.tomcat2.com

    ProxyPass / http://localhost:8081/
    SSLCertificateFile /certfiicate/for/server2.pem
</VirtualHost>

This way, all your installed webapps on the first Tomcat instance are available at https://www.tomcat1.com/<app_name>, and similarly, all your webapps on the second Tomcat instance are available at https://www.tomcat2.com/<app_name>.

Using AJP and mod_jk

If you want to use AJP for the connections, you have two options. The first one is mod_jk, what you use currently, so this is probably what you want. Using this, you can map the deployed webapps to their corresponding URLs, but cannot change the URLs, i.e. you can't make a mapping so that http://yourserver/thefrontend/ would lead to the webapp installed in the /thebackend context.

Using mod_jk, you should use this Apache config (your workers.properties file seems OK):

<VirtualHost *:443>
    ServerName demo.mydomain.com

    JkMount /* demo
    SSLCertificateFile /certfiicate/for/server1.pem
</VirtualHost>

<VirtualHost *:443>
    ServerName test.mydomain.com

    JkMount /* test
    SSLCertificateFile /certfiicate/for/server1.pem
</VirtualHost>

This way, everything on demo.mydomain.com will be proxied to the Tomcat instance named demo in the workers.properties file, and everything on test.mydomain.com will be proxied to the instance named test. You don't need to specify any Directory or Alias in order for this to work.

Using AJP and proxy_ajp

The other AJP connector you can use is proxy_ajp. Since you have already configured mod_jk, I think it is unlikely you want to use this instead, but I write it down, for the sake of completeness.

The AJP proxy Apache module doesn't need any extra properties file, you have to include the backend IP (or name) in the config. Your config would look like this:

<VirtualHost *:443>
    ServerName demo.mydomain.com

    ProxyPass / ajp://localhost:8009/
    SSLCertificateFile /certfiicate/for/server1.pem
</VirtualHost>

<VirtualHost *:443>
    ServerName test.mydomain.com

    ProxyPass / ajp://localhost:8010/
    SSLCertificateFile /certfiicate/for/server1.pem
</VirtualHost>

One advantage of proxy_ajp is that using this, you can map any context to any URL. For example, to map the /manager/ context to /the_great_manager/, you can use the following config:

ProxyPass /the_great_manager/ ajp://localhost:8009/manager/
ProxyPassReverse /the_great_manager/ /manager/
ProxyPassReverseCookiePath /the_great_manager/ /manager/

This can come in handy in certain circumstances, however, if you use redirects like the above, be ready for surprises. It is usually a good idea not to change the URL mapping unless you absolutely must.

Lacek
  • 7,233
  • 24
  • 28