首页 > 解决方案 > 阿帕奇 mod_jk | Virtualhost 不监听域名将请求转发到 Tomcats

问题描述

现在我的工作遇到了另一个问题。我已经调整了配置,现在,根据 URL,请求应该被转发到后端的 3 个 tomcat。

如果 URL 是www.mysite.com,则应将请求转发到 Tomcat 1,依此类推。

当然,它们是虚拟主机中的虚拟网站,或者至少我的服务器应该是这样认为的。但是,它不是那样工作的。如果我按预期访问 mysite.com,实际上会调用 mysite.com 而不是将请求转发到 Tomcat 1。

当我调用localhost时,直接显示Tomcat启动页面,实际上应该显示为localhost:8181。Apache 起始页应该实际出现。可以在 /app1..3 访问 Tomcat。所以这不是问题。

问题是:为了实现我的目标,我必须改变什么以及在哪里改变?

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>

“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

标签: apachetomcatvirtualhostmod-jk

解决方案


[您RewriteRule的 s 在模式和替换之间缺少一个空格。]

重写将发送到 Tomcat 的 URI 路径通常是一个坏主意:servlet 应用程序通常使用以下方法生成 URI

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

虽然 AJP 协议为 Tomcat 提供了 和 的正确值,<scheme>但Apache HTTP 服务器收到的原始值永远不会转发给 Tomcat。<serverName><serverPort><contextPath>

所以,例如:

  • 客户请求http://example.com/index.html
  • 您的重写规则将其更改为http://example.com/app1/index.html并将其发送到 Tomcat,
  • Tomcat 生成一个 HTML 页面,其图像位于http://example.com/app1/image.png,
  • 客户请求http://example.com/app1/image.png
  • 您的重写规则将其更改为http://example.com/app1/app1/image.png不存在的 。

一个简单的解决方案是将您的应用程序部署在客户端可以看到它们的相同上下文路径下:重命名所有应用程序ROOT以将它们部署在您网站的根目录下。


推荐阅读