hzrari
hzrari

Reputation: 1933

ProxyPassReverse doesn't rewrite Location (http header)

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

Upvotes: 2

Views: 4930

Answers (2)

Jack Yu
Jack Yu

Reputation: 2425

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.)

Upvotes: 1

Sharique Abdullah
Sharique Abdullah

Reputation: 655

Set it to this instead

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

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

Upvotes: 0

Related Questions