简体   繁体   中英

Apache server as a proxy using iptables

I want to accomplish the following: I want to show a task bar on top of all websites I request from my notebook. On this bar I display various information.

I have a Raspberry Pi in my network which I configured as a gateway on my notebook. So all the traffic from my notebook passes the raspberry. On the raspberry I installed an Apache server with two VirtualHosts. One is a local webserver listening on port 80 . For the other one, listening on port 8126 , I have the following rules:

  1. If no special parameter is set in the request, I redirect the request to a local page (with the original requested URI as a parameter). On this page I have an iframe in which I show the originally requested page, using the URI I read out from the parameters. To avoid having an endless loop I add the special parameter to the link in the iframe. This part works fine.

  2. If the special parameter is not set (since the page should be display in the iframe), I redirect the request to the originally requested page using *mod_rewrite* again. This redirecting seems to cause the problems.

So here is some code of what I am doing: I redirect all traffic on port 80 to port 8126 using iptables. 192.168.1.1 is the IP address of my raspberry which I use since I can not redirect to the loopback interface in the PREROUTING phase. I do this with the following iptables-rule:

iptables -t nat -A PREROUTING -i eth0 -p tcp 80 -j DNAT 192.168.1.1:8126

I boiled down my problem to the following VirtualHost configuration for the Apache server, where I simply redirect all requests to an external website:

<VirtualHost *:8126>
        RewriteEngine On
        RewriteRule ^/?(.*) http://example.com/ [P]
        ProxyPassReverse / http://example.com/
</VirtualHost>

On my notebook I set the gateway to 192.168.1.1 , open my Chrome browser and send an HTTP request to eg test.com . I would expect to get redirected to example.com . But I receive an error message saying

ERR_TOO_MANY_REDIRECTS

Does anybody have an idea how I could solve this? I am free for completely different approaches solving my problem!

Your iptables rule creates a loop so requests FROM the proxy are sent again to himself.

You should avoid that by excluding Proxy source IP from rule:

iptables -t nat -A PREROUTING -i eth0 -s ! <Your proxy IP> -p tcp 80 -j DNAT 192.168.1.1:8126

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM