本文介绍了ProxyPassReverse 不会重写位置(http 标头)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在前端服务器 (server1) 中安装了一个 apache,作为反向代理.我有另一台带有 tomcat 的服务器 (server2),它正在运行一个 web 应用程序.

I have an apache installed in a frontend server (server1), which is as reverse proxy. I have another server (server2) with tomcat that is running a webapp.

我像这样配置了我的反向代理(server1):

I configured my reverse proxy (server1) like that:

ProxyPass /app1/ ajp://server2:8009/app1/
ProxyPassReverse /app1/ https://www.external_domain_name.com/

当我连接到:

https://www.external_domain_name.com/app1/

我的网络应用程序运行正常.在某些页面中,网络应用将我 (302) 重定向到另一个页面.

my web app is working properly. In some pages, the web app redirects me (302) to another page.

然后,我被重定向到:

https://server1_internal_ip/app1/foo_bar

当我查看 http 标头时,响应标头包含:

When I look to the http headers, the response header contains:

Status code: 302
Location: https://server1_internal_ip/app1/foo_bar

所以,我的结论是 ProxyPass 工作正常,但 ProxyPassReverse 不是.

So, my conclusion ProxyPass is working properly, but the ProxyPassReverse is not.

你能帮我了解一下出了什么问题吗?

Can you help me please to understand what's going wrong?

谢谢

推荐答案

实际上 ProxyPassReverse 将替换您的服务器返回的 Location.

Actually ProxyPassReverse will replace the Location which your server returned.

Apache2 设置

ProxyPass "/8080" "http://localhost:8080"
ProxyPassReverse "/8080/" "/"

Node.js 设置

const express = require("express");
const app = express()

app.get('/', (req, res) => {
    res.json({a: 8080})
})

app.get("/hi", (req, res) => {
    res.json({a: "8080hi"})
})

app.get("/redirect", (req, res) => {
    res.redirect("/hi")
})

app.listen(8080)

原始位置是位置:/hi".
新的是位置:/8080/hi".(/=>/8080/)

Original Location is "Location: /hi".
New one is "Location: /8080/hi". (/ => /8080/)

这意味着 Apache2 用 ProxyPassReverse 设置替换了 Location 值.
或者您可以使用完整的 FQDN 来执行此操作.

That means Apache2 replaced the Location value with ProxyPassReverse setting.
Or you can use full FQDN to do it.

Apache2 设置

ProxyPass "/8080" "http://localhost:8080"
ProxyPassReverse "/8080" "http://localhost:8080"

Node.js 设置

const express = require("express");
const app = express()

app.get('/', (req, res) => {
    res.json({a: 8080})
})

app.get("/hi", (req, res) => {
    res.json({a: "8080hi"})
})

app.get("/redirect", (req, res) => {
    res.setHeader("Location", "http://localhost:8080/hi")
    res.send(302)
})

app.listen(8080)

Apache2 会将 http://localhost:8080/hi 转换为 http://localhost/8080/hi.
(如果我的 Apache2 配置为 80 端口.)

Apache2 will convert http://localhost:8080/hi to http://localhost/8080/hi.
(If my Apache2 is configured to 80 port.)

这篇关于ProxyPassReverse 不会重写位置(http 标头)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 00:05