apache mod_jk | Virtualhost does not listen the doimain name to forward the request to Tomcats

193 Views Asked by At

Now I have another problem on my work. I have adjusted the configuration so that now, depending on the URL, the request should be forwarded to the 3 tomcats in the backend.

If the URL is www.mysite.com, the request should be forwarded to Tomcat 1, and so on.

Of course, they're dummy websites in virtualhost, or at least that's what my server should think so. However, it doesn't work that way. If I go to mysite.com, as expected, mysite.com is actually called instead of the request being forwarded to Tomcat 1.

When I call up localhost, the Tomcat start page is displayed directly, which should actually be displayed for localhost:8181. The Apache start page should actually appear. The Tomcats can be reached at /app1..3. So that's not the problem.

The question is: what and where exactly do I have to change in order to achieve my goal?

httpd.conf

Listen 80       
NameVirtualHost *:80

<VirtualHost *:80>
        ServerName www.mysite.com
        RewriteEngine on
        RewriteRule ^/(.*)$/app1/$1 [l,PT]
        JkMount /* tomcat1
</VirtualHost>

<VirtualHost *:80>
        ServerName www.test.de
        RewriteEngine on
        RewriteRule ^/(.*)$/app2/$1 [l,PT]
        JkMount /* tomcat2
</VirtualHost>

<VirtualHost *:80>
        ServerName www.example.de
        RewriteEngine on
        RewriteRule ^/(.*)$/app3/$1 [l,PT]
        JkMount /* tomcat3
</VirtualHost>

output of "httpd -S"

AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/httpd/conf/httpd.conf:48
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server www.mysite.com (/etc/httpd/conf/httpd.conf:50)
         port 80 namevhost www.mysite.com (/etc/httpd/conf/httpd.conf:50)
         port 80 namevhost www.test.de (/etc/httpd/conf/httpd.conf:57)
         port 80 namevhost www.example.de (/etc/httpd/conf/httpd.conf:64)
ServerRoot: "/etc/httpd"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/etc/httpd/logs/error_log"
Mutex lua-ivm-shm: using_defaults
Mutex proxy: using_defaults
Mutex authn-socache: using_defaults
Mutex default: dir="/etc/httpd/run/" mechanism=default 
Mutex cache-socache: using_defaults
Mutex authdigest-opaque: using_defaults
Mutex watchdog-callback: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
Mutex authdigest-client: using_defaults
PidFile: "/etc/httpd/run/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="apache" id=48
Group: name="apache" id=48
1

There are 1 best solutions below

6
Piotr P. Karwasz On

[Your RewriteRules are missing a space between the pattern and the substitution.]

It is usually a bad idea to rewrite URI paths that will be sent to Tomcat: servlet applications commonly generate URIs using

<scheme>://<serverName>:<serverPort>/<contextPath>/relative/path/to/resource

While the AJP protocol provides Tomcat with the correct values of <scheme>, <serverName> and <serverPort>, the original value of <contextPath> received by the Apache HTTP Server is never forwarded to Tomcat.

So, e.g.:

  • a client requests http://example.com/index.html,
  • your rewrite rule changes it to http://example.com/app1/index.html and sends it to Tomcat,
  • Tomcat generates a HTML page with an image located at http://example.com/app1/image.png,
  • the client requests http://example.com/app1/image.png
  • your rewrite rule changes it to http://example.com/app1/app1/image.png, which does not exist.

An easy solution is to deploy your application under the same context path under which they will be seen by the clients: rename all applications as ROOT to deploy them at the root of your website.