0

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
ng-User
  • 243
  • 1
  • 6
  • 18

1 Answers1

1

[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.

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • thanks, correct, there was no spaces. Ok, I understand your solution, but in my case, why I get the real website content of mysite.com or www.test.de instead of the content of tomcat webapp. Do I have to change or add something in "/etc/hosts" ? – ng-User May 20 '21 at 16:26
  • And http://localhost/ is forwarded directly to the first Tomcat 1, instead of apache main page – ng-User May 20 '21 at 17:22
  • Maybe you have multiple virtual hosts for the same name. What does `apache2ctl -S` or `httpd -S` say? – Piotr P. Karwasz May 20 '21 at 17:31
  • I have added the output to my origin Question above. – ng-User May 20 '21 at 18:35
  • 1
    The virtualhost configuration looks, Ok. Sorry, I didn't read your first comment carefully: of course `/etc/hosts` must contain and entry for `mysite.com` that point to your computer, e.g. `127.0.2.1 www.mysite.com www.test.de www.example.de` – Piotr P. Karwasz May 21 '21 at 04:22
  • 1
    OK, problem solved. I had edited /etc/hosts before, it didn't work anyway. The real problem was the cache of my browser!! I had to delete it, now I don't get the content of the real websites test.de etc. Thanks for your hints and effort!! – ng-User May 22 '21 at 12:51