ProxyPassReverse doesn't rewrite Location (http header)

泄露秘密 提交于 2020-07-19 06:28:26

问题


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.

I configured my reverse proxy (server1) like that:

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

When I connect to:

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

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

Then, I am redirected to :

https://server1_internal_ip/app1/foo_bar

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

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

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

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

Thanks


回答1:


Actually ProxyPassReverse will replace the Location which your server returned.

Example 1 (Only URL Path)

Apache2 Setting

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

Node.js Setting

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)

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

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

Example 2 (FQDN)

Apache2 Setting

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

Node.js Setting

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 will convert http://localhost:8080/hi to http://localhost/8080/hi.
(If my Apache2 is configured to 80 port.)




回答2:


Set it to this instead

ProxyPassReverse /app1/ ajp://server2:8009/app1/

Seemed to work for me when I came across a similar problem.



来源:https://stackoverflow.com/questions/19454271/proxypassreverse-doesnt-rewrite-location-http-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!