익명 07:33

Apache server behind reverse proxy redirecting incorrectly

Apache server behind reverse proxy redirecting incorrectly

Let's say I have a reverse proxy server (proxy) and a backend server actually handling the traffic (backend). I use a reverse SSH tunnel to send the traffic to the backend server.

So I setup the proxy server like this:

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

I enabled the correct Apache modules, and all was good.

However, I noticed an issue. If I specified a redirect like so:

Redirect permanent /index.html /w/index.php

Accessing the backend server without the proxy causes the redirect to work correctly (e.g. http://backend redirects to http://backend/w/index.php), but accessing it through the proxy server causes it to redirect to http://localhost/w/index.php instead, which shouldn't be happening.

My SSH tunnel runs on the backend server, and looks something like this:

ssh -NT -R localhost:8080:localhost:80 proxy-server

Edit: I also tried using ProxyPreserveHost On on the proxy server, but what this seemed to do is redirect me from http://proxy to http://backend/w/index.php; this is not desired because the backend server will not be open to the public.

Would setting ServerName or creating a VirtualHost or something here help?



Top Answer/Comment:

Adding a PreserveProxyHost On fixed the problem. I confirmed using Telnet on port 80 to send an HTTP request by hand, and when I change the Host header, the redirect URL changes too.

I faced another issue after that, which was that MediaWiki (which runs on /w/) was redirecting me wrong, which was solved by setting MediaWiki's $wgServer to proxy (it had been set to backend by the automated installer script).

상단 광고의 [X] 버튼을 누르면 내용이 보입니다