如何让tomcat 5.5在apache 2后面运行,mod_rewrite将请求传递给mod_jk并剥离应用程序上下文?

时间:2022-06-21 16:55:05

Ok, so I want to get a webapp running in tomcat (5.5) to run behind apache 2 (2.2.3 to be precise) serving from the root of the site (i.e. without the context), with static content being served via apache.

好吧,所以我想让一个在tomcat(5.5)中运行的webapp运行在apache 2(确切地说是2.2.3)服务于站点的根目录(即没有上下文),静态内容通过apache提供。

So if the app is running under "/myapp" on tomcat I want to use apache (plus mod_rewrite) to make it behave as if it's running under "/" instead.

因此,如果应用程序在tomcat上的“/ myapp”下运行,我想使用apache(加上mod_rewrite)使其行为就好像它在“/”下运行一样。

Mod_jk is setup and working ok. I can access the app from "/myapp", but I can't quite get the last bit working. Below is the config I've got for mod_rewrite to try and get this working. It correctly gets rewrites /static/ urls to get apache to serve them from the unpacked webapp and if I enable the rewrite log I see that it does attempt to pass through all other requests to /myapp via mod_jk. However it seems that mod_jk is not processing the request afterwards.

Mod_jk已设置好并且正常工作。我可以从“/ myapp”访问该应用程序,但我无法完成最后一点工作。下面是我为mod_rewrite设置的配置,试图让它工作。它正确地获取重写/静态/ url以获取apache从解压缩的webapp服务它们,如果我启用重写日志,我看到它确实尝试通过mod_jk将所有其他请求传递给/ myapp。但是,似乎mod_jk之后没有处理请求。


JkMount /myapp/*      worker1

RewriteEngine On

# ensure static stuff gets served by apache
RewriteRule ^/static/(.*)$ /var/lib/tomcat5.5/webapps/myapp/static/$1 [L]
# everything else should go through tomcat
RewriteRule ^/(.*)$ /myapp/$1 [L,PT]

When I've done this with apache 1 in the past I've had to make sure mod_jk get's loaded before mod_rewrite, but I can't seem to achieve this under apache 2. Any thoughts? How do other people usually do this?

当我在过去使用apache 1完成此操作时,我必须确保在mod_rewrite之前加载mod_jk,但我似乎无法在apache 2下实现这一点。任何想法?其他人通常如何做到这一点?

4 个解决方案

#1


1  

Managed to get this working in the end. It appears that I need to set a JkOption to:

管理最终使这项工作。看来我需要将JkOption设置为:


JkOptions     +ForwardURICompat

And then mod_jk looks at the rewritten URL. Must admit not 100% sure quite why this works, but I believe it's reverting to an older form of behaviour. If anyone can tell me a better/more correct way of doing this I'd love to know.

然后mod_jk查看重写的URL。必须承认不是100%确定为什么这样做,但我相信它正在回归到一种较老的行为形式。如果有人能告诉我更好/更正确的做法,我很想知道。

#2


1  

May be better use Apache for proxy instead of mod_jk. Something like this:

可能更好地使用Apache代理而不是mod_jk。像这样的东西:

ProxyPass /static/ http://localhost:8080/myapp/static/

ProxyPass / static / http:// localhost:8080 / myapp / static /

#3


0  

It might be easier or more transparent what happens if you use either Tomcat Virtual Hosts or different connectors for different hosts and just deploy root applications (at "/") and setup Apache mod_jk forwarding to the different connectors or virtual hosts.

如果您为不同的主机使用Tomcat虚拟主机或不同的连接器,只是部署根应用程序(在“/”)并将Apache mod_jk转发到不同的连接器或虚拟主机,则可能会更容易或更透明。

Note: When I needed this I've worked with different connectors for different tomcat hosts or Engines (can't remember) and deployed ROOT applications. I've never tried virtual hosts (name based) in tomcat, only guessing that this could work.

注意:当我需要这个时,我使用不同的连接器为不同的tomcat主机或引擎(不记得)和部署的ROOT应用程序。我从来没有尝试过tomcat中的虚拟主机(基于名称),只是猜测这可行。

Benefit: no path translation, thus a lot easier to understand once you have to make changes to the installation months after initial deployment.

好处:没有路径转换,因此一旦在初始部署后必须对安装进行更改,就会更容易理解。

I confess, it feels somewhat strange, but the benefit of readability is worth a lot to me.

我承认,感觉有些奇怪,但可读性的好处对我来说非常值得。

#4


-1  

We use the 'R' flag instead of 'PT':

我们使用'R'标志而不是'PT':

RewriteRule ^/(.*)$ /myapp/$1 [L,R]

Edit: I missed the point to not alter the URL the user sees. An alternative way is to do:

编辑:我错过了不改变用户看到的URL的观点。另一种方法是:

JkMount /* worker1
JkUnmount /static/* worker1

Then you won´t need the RewriteRule's.

那你就不需要RewriteRule了。

And according to Apache Tomcat Site the new default settings of the mod_jk are incompatible with mod_rewrite and you should use +ForwardURICompatUnparsed.

根据Apache Tomcat Site,mod_jk的新默认设置与mod_rewrite不兼容,您应该使用+ ForwardURICompatUnparsed。

#1


1  

Managed to get this working in the end. It appears that I need to set a JkOption to:

管理最终使这项工作。看来我需要将JkOption设置为:


JkOptions     +ForwardURICompat

And then mod_jk looks at the rewritten URL. Must admit not 100% sure quite why this works, but I believe it's reverting to an older form of behaviour. If anyone can tell me a better/more correct way of doing this I'd love to know.

然后mod_jk查看重写的URL。必须承认不是100%确定为什么这样做,但我相信它正在回归到一种较老的行为形式。如果有人能告诉我更好/更正确的做法,我很想知道。

#2


1  

May be better use Apache for proxy instead of mod_jk. Something like this:

可能更好地使用Apache代理而不是mod_jk。像这样的东西:

ProxyPass /static/ http://localhost:8080/myapp/static/

ProxyPass / static / http:// localhost:8080 / myapp / static /

#3


0  

It might be easier or more transparent what happens if you use either Tomcat Virtual Hosts or different connectors for different hosts and just deploy root applications (at "/") and setup Apache mod_jk forwarding to the different connectors or virtual hosts.

如果您为不同的主机使用Tomcat虚拟主机或不同的连接器,只是部署根应用程序(在“/”)并将Apache mod_jk转发到不同的连接器或虚拟主机,则可能会更容易或更透明。

Note: When I needed this I've worked with different connectors for different tomcat hosts or Engines (can't remember) and deployed ROOT applications. I've never tried virtual hosts (name based) in tomcat, only guessing that this could work.

注意:当我需要这个时,我使用不同的连接器为不同的tomcat主机或引擎(不记得)和部署的ROOT应用程序。我从来没有尝试过tomcat中的虚拟主机(基于名称),只是猜测这可行。

Benefit: no path translation, thus a lot easier to understand once you have to make changes to the installation months after initial deployment.

好处:没有路径转换,因此一旦在初始部署后必须对安装进行更改,就会更容易理解。

I confess, it feels somewhat strange, but the benefit of readability is worth a lot to me.

我承认,感觉有些奇怪,但可读性的好处对我来说非常值得。

#4


-1  

We use the 'R' flag instead of 'PT':

我们使用'R'标志而不是'PT':

RewriteRule ^/(.*)$ /myapp/$1 [L,R]

Edit: I missed the point to not alter the URL the user sees. An alternative way is to do:

编辑:我错过了不改变用户看到的URL的观点。另一种方法是:

JkMount /* worker1
JkUnmount /static/* worker1

Then you won´t need the RewriteRule's.

那你就不需要RewriteRule了。

And according to Apache Tomcat Site the new default settings of the mod_jk are incompatible with mod_rewrite and you should use +ForwardURICompatUnparsed.

根据Apache Tomcat Site,mod_jk的新默认设置与mod_rewrite不兼容,您应该使用+ ForwardURICompatUnparsed。