Search code examples
http-redirectproxyroutesiptablessquid

Iptables redirect to remote squid proxy


I want to redirect any http traffic generated from local host to a remote squid.

HTTP server located in one data center, if any web based(http) outbound request is generated, it should be redirect to remote squid server(located in a remote data center). I have have verified that squid proxy is working fine.

HTTP server has one interface eth0. I have write following iptables rule but it is not working.

Web server IP = 192.168.1.1
Proxy Server IP/Port = publicip:3128

iptables -t nat -A PREROUTING -p tcp -s 192.168.1.1 --dport 80 -j DNAT --to-destination  publicip:3128
iptables -t nat -A POSTROUTING -j MASQUERADE

But above rules are not working.


Solution

  • As far as I understand your question, the traffic is generated locally. Thus, packets won't traverse the PREROUTING chain. You have to work on OUTPUT or POSTROUTING :

    iptables -t nat -A OUTPUT -p tcp -o lo --dport 80 -j DNAT --to publicip:3128
    

    Also, be sure to set-up your Squid in transparent proxying mode :

    httpd_accel_host virtual
    httpd_accel_port 80
    httpd_accel_with_proxy on
    httpd_accel_uses_host_header on
    

    If it's still not working, pleas provide additional details on what is not working (tcpdump traces, squid logs).