2017-09-04 60 views
5

我必须将80上的所有apache请求重定向到8080上的tomcat,除了一个路径。Apache ProxyPass错误

所以,如果收到http://example.com/anything - > tomcat:8080。

但是,如果URL是:http://example.com/site - > apache应该服务,并且不需要重定向。

目前,在/var/www/html/内有一个名为site的文件夹。

这是我目前的配置文件:

site.conf(这个文件只包含以下内容,是conf.d文件夹中)

<LocationMatch "/*"> 
     Allow from all 
     ProxyPass    /site ! 
     ProxyPass    http://127.0.0.1:8080 
     ProxyPassReverse  http://127.0.0.1:8080 
</LocationMatch> 

我觉得这是一个简单的事情来完成与Apache,但我已经尝试了一切,我可以找到,我仍然得到错误:

ProxyPass|ProxyPassMatch can not have a path when defined in a location. 

问题是根网站运行在tomcat上,但其他运行在apache上(我在这个问题中调用了站点)。

如果有人可以帮忙,我很感激。

谢谢!

更新1 - 2017年9月6日

我得到它,如果我删除LocationMatch,把ProxyPass 直接在.conf文件工作:

ProxyPass    /site ! 
ProxyPassReverse  /site ! 
ProxyPass    /http://127.0.0.1:8080 
ProxyPassReverse  /http://127.0.0.1:8080 

不过,我会想知道,为什么呢?将此指令放在LocationMatch标签之外的影响是什么?而且,最重要的是,为什么我不能使用LocationMatch完成相同的结果?

回答

4

我认为错误是相当清楚的:

ProxyPass|ProxyPassMatch can not have a path when defined in a location. 

the documentation,像LocationLocationBlock上下文块内的ProxyPass指令不接受一个路径:

When used inside a <Location> section, the first argument is omitted and the local directory is obtained from the <Location> . The same will occur inside a <LocationMatch> section; however, ProxyPass does not interpret the regexp as such, so it is necessary to use ProxyPassMatch in this situation instead.

你因为您尝试使用路径而出现错误:

ProxyPass    /site ! 

您可以尝试通过使用多个<Location>部分,这样在理论上解决此问题:

<Location /> 
    ProxyPass http://backend/ 
</Location> 

<Location /site> 
    ProxyPass ! 
</Location> 

ordering of these sections is important

您在LocationMatch块之外使用ProxyPass指令的解决方案可能是最简单的解决方案。请注意,您的LocationMatch指令是不正确的。 LocationMatch的参数是一个正则表达式,而/*只会匹配仅包含/个字符的网址。也就是说,它会匹配////////////等。我认为你的意思是/.*。正则表达式中的*表示“前一个字符,零次或多次”。

+0

好吧,我明白了。谢谢!我会使用'Location'来尝试你的解决方案。但是,通过不使用“位置”,有什么区别?在文件中直接设置这些代理值与“VirtualHost”本身或“Directory”的设置相同?这种方法有什么问题? – Luiz

+0

如果只使用'Location'块来设置'ProxyPass',我不确定使用'Location'与使用配置文件顶层的'ProxyPass'有很大区别。我猜如果你在'Location'部分做的不仅仅是'ProxyPass',你可能更愿意将相关的配置元素放在一起。 – larsks