Saturday, July 16, 2016

amazon ec2 - preventing squid forwarding loop with IPtables



I have a web application, and I'd like to limit file upload size.



I thought I'd try using squid for this, via its directive request_body_size_max.



I have my webserver running on port 8080, and I've installed squid 3.1.19 on the same server (Ubuntu 12.04 LTS). The server is running on Amazon's EC2, if that makes any difference.



squid.conf contains:




http_port 3128 transparent


I added to IPTables:



iptables -t nat -A PREROUTING -p tcp --dport 8080 -j REDIRECT --to-port 3128


When I visit the webserver, the result in squid's logs, is:




Forwarding loop detected.


What do I need to do to prevent this? thanks...


Answer



The problem is when squid tries to connect to the web server (on port 8080) it's request is also redirected to itself. You must limit the scope of the REDIRECT rule (e.g. based on the incoming interface).



If the outside interface on your server is eth0 change the rule like this:




iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 8080 -j REDIRECT --to-port 3128


Update: On the second thought looks like I'm wrong. Local (127.0.0.1) traffic doesn't go through the PREROUTING chain.



How did you tell squid what upstream web server to use?


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...