PHP+Apache as forward/reverse proxy: ¿how to process client requests and server responses in PHP?

Posted by Lightworker on Server Fault See other posts from Server Fault or by Lightworker
Published on 2011-01-14T20:22:34Z Indexed on 2011/01/15 12:55 UTC
Read the original article Hit count: 587

Filed under:
|
|
|
|

Hi! I'm having a lot of troubles with the propper configuration of Apache mod_proxy.so to work as desired...

The main idea, is to create a proxy on a local machine in a network wich will have the ability to proces a client request (client connected through this Apache prepared proxy) in PHP. And also, it will have the capacity to process the server responses on PHP too.

Those are the 2 funcionalities, and they are independent one from each other.

Let me present a little schema of what I need to achive:

alt text

As you can see here, there're 2 ways: blue one and red one.

For the blue one, I basically conected a client (Machine B - cell phone) on my local network (home) and configured it to go thorugh a proxy, wich is the Machine A (personal computer) on the exactly same network.

So let's say (not DHCP):

Machine A: 192.168.1.40 --> Apache is running on this machine, and configured to listen port 80.

Machine B (cell phone): 192.168.1.75 --> configured to go throug a proxy, wich is IP 192.168.1.75 and port 80 (basically, Machine A).

After configuring Apache properly, wich is basically to remove the "#" from httpd.conf on the lines for the mod_proxy.so (main worker), mod_proxy_connect.so (SSL, allowCONNECT, ...) and mod_proxy_http.so (needed for handle HTTP request/responses) and having in my case, lines like this:

# Implements a proxy/gateway for Apache.
Include "conf/extra/httpd-proxy.conf"

# Various default settings
Include "conf/extra/httpd-default.conf"

# Secure (SSL/TLS) connections
Include "conf/extra/httpd-ssl.conf"

wich gives me the ability to configure the file httpd-proxy.conf to prepare the forward proxy or the reverse proxy.

So I'm not sure, if what I need it's a forward proxy or a reverse one.

For a forward proxy I've done this:

<IfModule proxy_module>
<IfModule proxy_http_module>

#
# FORWARD Proxy
#

#ProxyRequests Off
ProxyRequests On
ProxyVia On

<Proxy *>
    Order deny,allow
#   Allow from all
    Deny from all
    Allow from 192.168.1
</Proxy>

</IfModule>
</IfModule>

wich basically passes all the packets normally to the server and back to the client. I can trace it perfectly (and testing that works) looking at the "access.log" from Apache. Any request I make with the cell phone, appears then on the Apache log. So it works.

But here come the problem:

  • I need to process those client requests. And I need to do it, in PHP.

I have read a lot about this. I've read in detail the oficial site from Apache about mod_proxy. And I've searched a lot on forums, but without luck.

So I thought about a first aproximation:

1) Forward proxy in Apache, passes all the packets and it's not possible to process them. This seems to be true, so, what about a reverse proxy?

So I envisioned something like:

ProxyRequests Off

<Proxy *>
Order deny,allow
Allow from all
</Proxy>

ProxyPass http://www.google.com http://www.yahoo.com
ProxyPassReverse http://www.google.com http://www.yahoo.com 

which is just a test, but this should cause on my cell phone that when trying to navigate to Google, I should be going to Yahoo, isn't it? But not. It doesn't work.

So you really see, that ALL the examples on Apache reverse proxy, goes like:

ProxyPass /foo http://foo.example.com/bar
ProxyPassReverse /foo http://foo.example.com/bar

wich means, that any kind of request in a local context, will be solved on a remote location.

But what I needed is the inverse! It's that when asking for a remote site on my phone, I solve this request on my local server (the Apache one) to process it with a PHP module.

So, if it's a forward proxy, I need to pass through PHP first. If it's a reverse proxy, I need to change the "going" direction to my local server one to process first on PHP.

Then comes in mind second option:

2) I've seen something like:

<Proxy http://example.com/foo/*>
SetOutputFilter INCLUDES
</Proxy>

And I started to search for SetOutputFilter, SetInputFilter, AddOutputFilter and AddInputFilter.

But I don't really know how can I use it.

Seems to be good, or a solution to me, cause with somethin' like this, I should can add an Input filter to process on PHP the client requests and send back to the client what I programed/want (not the remote server response) wich is the BLUE path on schema, and I should have the ability to add an Output filter wich seems to give me the ability to process the remote server response befor sending it to the client, wich should be the RED path on the schema.

Red path, it's just to read server responses and play with em. But nothing more. The Blue path, it's the important one. Cause I will send to the client whatever I want after procesing the requests.

I so sorry for this amazingly big post, but I needed to explain it as well as I can.

I hope someone will understand my problem, and will help me to solve it!

Lot of thanks in advance!! :)

© Server Fault or respective owner

Related posts about php

Related posts about apache