使用Tomcat + Mod_jk的虚拟主机

时间:2014-12-19 14:03:47

标签: apache tomcat vhosts mod-jk

我在我的cent os服务器上有一个tomcat(在8080上运行)应用程序,我可以使用url访问它:

SERVER_IP:8080/myapp

我在

上有我的管理面板
SERVER_IP:8080/myapp/admin

我在端口80上运行了一个apache服务器。

我有两个域名:

www.myapp.com
admin.myapp.com

我已设法配置tomcat,mod_jk和apache服务器,以便admin.myapp.com url打开tomcat主页(SERVER_IP:8080)。

但我想要的是让admin.myapp.com网址打开SERVER_IP:8080 / myapp / admin和www.myapp.com打开SERVER_IP:8080 / myapp。

我需要让apache知道何时向admin.myapp.com发出请求,它应该知道将请求重定向到SERVER_IP:8080 / myapp / admin。显然有些东西丢失了。

以下是我的配置:

httpd.conf(admin.myapp.com.conf实际上是因为它是由plesk小组创建的,但我认为它是无效的)

<VirtualHost SERVER_IP:80 >
    ServerName "admin.myapp.com:80"
    ServerAlias "www.admin.myapp.com"
    ServerAlias "ipv4.admin.myapp.co"
    ServerAdmin "cuneyty@mycompany.com"
    UseCanonicalName Off

    JkMount / ajp13
    JkMount /* ajp13

    ....
</VirtualHost>

mod_jk.conf

# Load mod_jk module
# Update this path to match your modules location
LoadModule jk_module modules/mod_jk.so

# Where to find workers.properties
# Update this path to match your conf directory location
JkWorkersFile /usr/local/tomcat7/conf/workers.properties

# Where to put jk logs
# Update this path to match your logs directory location
JkLogFile /usr/local/tomcat7/logs/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel debug

# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"

# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

workers.properties

# Define 1 real worker named ajp13
worker.list=ajp13

# Set properties for worker named ajp13 to use ajp13 protocol,
# and run on port 8009
worker.ajp13.type=ajp13
worker.ajp13.host=localhost
worker.ajp13.port=8009
worker.ajp13.lbfactor=50
worker.ajp13.cachesize=10
worker.ajp13.cache_timeout=600
worker.ajp13.socket_keepalive=1
worker.ajp13.socket_timeout=300

提前致谢。

2 个答案:

答案 0 :(得分:1)

实现这一目标的最简单方法是使用mod_proxy
在httpd.conf中加载mod_proxy模块

<VirtualHost SERVER_IP:80>

     ServerName admin.myapp.com
     ServerAlias www.myapp.com
     ServerAlias www.admin.myapp.com
     ServerAlias ipv4.admin.myapp.co
     ServerAdmin cuneyty@mycompany.com
     # Any additional configuration/customization.

     RewriteEngine on
     RewriteCond %{HTTP_HOST} ^admin\.myapp\.com$ [NC]
     RewriteRule ^(.*)$ http://SERVER_IP:8080/myapp/admin/$1 [R=301,L]

     ProxyRequests Off    
     ProxyPass / ajp://SERVER_IP:8009/
     ProxyPass /myapp/ ajp://SERVER_IP:8009/myapp


</VirtualHost>

您还可以通过启用mod_proxy_balancer来实现负载平衡。它是mod_proxy的扩展,用于负载平衡。

查看mod_proxymod_jk here的优缺点

祝你好运!

答案 1 :(得分:0)

毕竟,我想出了这个解决方案,使用mod_proxy而不是mod_jk作为另一个答案中提到的斜杠。但对我有用的是如下:

ProxyPass / http://SERVER_IP:8080/ 
ProxyPass /myapp/ http://SERVER_IP:8080/myapp

然后我添加了以下关于cookie的行:

ProxyPassReverseCookiePath /myapp /