The business cases for this kind of functionality are several. I, specifically, want to move a set of Web services from one host to another while keeping the old URLs working. Since those are services, meant to be invoked by programs and not people, redirection won't work. Another possible scenario involves exposing an HTTP service from behind a firewall without exposing the whole host.
There are built-in means for creating reverse proxies both in Apache and in Internet Information Server. The problem is, leveraging those requires administrative rights, and with certain Web hosting packages, one might not have them.
On the other hand, there's nothing magical about forwarding an HTTP request. A piece of server side code (PHP in my case) is perfectly capable of issuing an outgoing HTTP request, passing the incoming request headers along, then sending the response headers and data back. So I went ahead and wrote one.
The reverse proxy script is done, it works (for me) and I don't mind sharing.
If you're interested in this script, here are the installation steps.
- Download this archive from Dropbox
- Upzip into a folder on a Web server
- Open proxy_config.php, describe the location(s) under which the proxy is sitting and which targets they should invoke
- Open .htaccess, change the value of RewriteBase to reflect the URI location where the proxy is sitting (multiple lines might or might not work, test it)
- Make log.txt writable to the world
If rewrite is not available, there's still a way to run the proxy. I won't go into that here, but the idea is either establishing symlinks to proxy.php all over the proxy folder, or placing renamed copies of rproxy.php all over the folder. It's not pretty, but it'll work. Static content needs to be duplicated outright (or a handler needs to be established).
In addition to the most basic HTTP functionality, the proxy supports:
- Passing headers back and forth as much as reasonably possible - so caching instructions, content type, user agent and such won't be lost
- Arbitrary HTTP methods (i. e. REST)
- POST/PUT/PATCH data in arbitrary format - not just forms
- Cookies and sessions, unless the target uses path-specific cookies nontrivially
- HTTPS -if you designate the target as protocol independent (with no leading http://)
- Proxy folders that are accessible via several URIs
- Absolute URLs in HTML
- Redirection to absolute URLs within the same site
- Domain- and path-specific cookies might break, depending on the way the target works
It probably doesn't scale well with content size. Rather than passing the bytes to the client as soon as they arrive from the target, the script stores the whole thing in memory.
The preferred environment is PHP 5 under Apache. In theory, the script should work under other environments too, but I had little chance to test it under those.