Apache - 反向代理和 HTTP 302 状态消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/189451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:51:32  来源:igfitidea点击:

Apache - Reverse Proxy and HTTP 302 status message

apacheredirectreverse-proxyhttp-status-code-302

提问by Rob

My team is trying to setup an Apache reverse proxy from a customer's site into one of our web applications.

我的团队正在尝试将 Apache 反向代理从客户站点设置到我们的 Web 应用程序之一。

http://www.example.com/app1/some-pathmaps to http://internal1.example.com/some-path

http://www.example.com/app1/some-path映射到http://internal1.example.com/some-path

Inside our application we use struts and have redirect = true set on certain actions in order to provide certain functionality. The 302 status messages from these re-directs cause the user to break out of the proxy resulting in an error page for the end user.

在我们的应用程序中,我们使用 struts 并在某些操作上设置了 redirect = true 以提供某些功能。来自这些重定向的 302 状态消息会导致用户脱离代理,从而导致最终用户出现错误页面。

HTTP/1.1 302 Found Location: http://internal.example.com/some-path/redirect

HTTP/1.1 302 找到位置:http: //internal.example.com/some-path/redirect

Is there any way to setup the reverse proxy in apache so that the redirects work correctly?

有没有办法在 apache 中设置反向代理,以便重定向正常工作?

http://www.example.com/app1/some-path/redirect

http://www.example.com/app1/some-path/redirect

回答by Kevin Hakanson

There is an article titled Running a Reverse Proxy in Apachethat seems to address your problem. It even uses the same example.com and /app1 that you have in your example. Go to the "Configuring the Proxy" section for examples on how to use ProxyPassReverse.

有一篇题为在 Apache运行反向代理的文章似乎解决了您的问题。它甚至使用与示例中相同的 example.com 和 /app1。有关如何使用ProxyPassReverse 的示例,请转到“配置代理”部分。

回答by Marcel Levy

The AskApache articleis quite helpful, but in practice I found a combination of Rewrite rules and ProxyPassReverse to be more flexible. So in your case I'd do something like this:

AskApache文章是非常有帮助的,但在实践中,我发现改写的组合规则和ProxyPassReverse更加灵活。所以在你的情况下,我会做这样的事情:

    <VirtualHost example>
       ServerName www.example.com

       ProxyPassReverse /app1/some-path/ http://internal1.example.com/some-path/
       RewriteEngine On
       RewriteRule /app1/(.*)   http://internal1.example.com/some-path [P]

       ...
    </VirtualHost>

I like this better because it gives you finer-grained control over the paths you're proxying for the internal server. In our case we wanted to expose only part of third-party application. Note that this doesn't address hard-coded links in HTML, which the AskApache article covers.

我更喜欢这个,因为它可以让您更精细地控制您为内部服务器代理的路径。在我们的例子中,我们只想公开第三方应用程序的一部分。请注意,这并没有解决 AskApache 文章所涵盖的 HTML 中的硬编码链接。

Also, note that you can have multiple ProxyPassReverse lines:

另请注意,您可以有多个 ProxyPassReverse 行:

    ProxyPassReverse / http://internal1.example.com/some-path
    ProxyPassReverse / http://internal2.example.com/some-path

I mention this only because another third-party app we were proxying was sending out redirects that didn't include their internal host name, just a different port.

我提到这一点只是因为我们代理的另一个第三方应用程序发送的重定向不包含其内部主机名,只是一个不同的端口。

As a final note, keep in mind that Firebugis extremely useful when debugging the redirects.

最后要注意的是,请记住Firebug在调试重定向时非常有用。

回答by philippn

Basically, ProxyPassReverseshould take care of rewriting the Location header for you, as Kevin Hakanson pointed out.

基本上,ProxyPassReverse正如 Kevin Hakanson 指出的那样,应该为您重写 Location 标头。

One pitfall I have encountered is missing the trailing slash in the url argument. Make sure to use:

我遇到的一个陷阱是缺少 url 参数中的尾部斜杠。确保使用:

ProxyPassReverse / http://internal1.example.com/some-path/

(note the trailing slash!)

(注意尾部斜杠!)

回答by James Schek

Try using the AJP connector instead of reverse proxy. Certainly not a trivial change, but I've found that a lot of the URL nightmares go away when using AJP instead of reverse proxy.

尝试使用 AJP 连接器而不是反向代理。当然不是一个微不足道的变化,但我发现当使用 AJP 而不是反向代理时,很多 URL 噩梦都会消失。