July 12, 2009 by  

[HowTo] Install and Configure Squid as Transparent Proxy

Squid cache logoSquid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-requested web pages. Squid has extensive access controls and makes a great server accelerator.

An intercepting proxy (also known as a “transparent proxy“) combines a proxy server with a gateway. Connections made by client browsers through the gateway are redirected through the proxy without client-side configuration (or often knowledge). So the client never realize and don’t have to configure the client machine to use the proxy, but they are using it.

Squid Cache Proxy Installation

1. Open up your shell and type this command:

sudo apt-get install squid

2. Finish.
For other OS you can download the binary package here.

Configure Squid Cache Proxy as Transparent Proxy

To configure squid proxy as transparent proxy you need to edit squid.conf file in /etc/squid/squid.conf as follow:

acl all src all
acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl localnet src 192.168.1.0/24
 
acl SSL_ports port 443 563
acl Safe_ports port 80        # http
acl Safe_ports port 21        # ftp
acl Safe_ports port 443      # https
acl Safe_ports port 70        # gopher
acl Safe_ports port 210      # wais
acl Safe_ports port 1025-65535    # unregistered ports
acl Safe_ports port 280        # http-mgmt
acl Safe_ports port 488        # gss-http
acl Safe_ports port 591        # filemaker
acl Safe_ports port 777        # multiling http
acl CONNECT method CONNECT
 
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
 
http_access allow localnet
http_access allow localhost
http_access deny all
 
http_reply_access allow localnet
http_reply_access deny all
 
icp_access allow localnet
icp_access deny all
 
http_port 8080 transparent
 
hierarchy_stoplist cgi-bin ?
 
cache_mem 256 MB
cache_dir ufs /var/spool/squid 2048 16 256
cache_mgr admin@email.com
cache_effective_user squid
cache_effective_group squid
 
access_log /var/log/squid/access.log squid
 
refresh_pattern ^ftp:        1440    20%    10080
refresh_pattern ^gopher:    1440    0%    1440
refresh_pattern (cgi-bin|\?)    0    0%    0
refresh_pattern .        0    20%    4320
 
visible_hostname yourdomain.com
 
icp_port 3130
 
always_direct    allow    all
 
forwarded_for off
 
coredump_dir /var/spool/squid

The most important line is
“http_port 8080 transparent” : This line means, Squid proxy run as transparent proxy at port 8080 (by default 3128). Later you need to edit the iptables to bypass every request/response connection through this port.
Note: That setting is for Squid v2.6 or v2.7. For later version like Squid v3.1, “transparent” option is being deprecated, you need to use option “intercept” instead.

There are many things that squid can do, like limiting download speed for certain ip, denied some “time wasting” sites, denied some ports, denied download some files in certain hours, and many more case that you can name. So take your time to read their documentation guide here.

Note: Squid Web Proxy installation steps above only for Ubuntu/Debian. For others it might working but need adjustment.

Iptables Configurations

To make Squid as the transparent proxy (“man in the middle”), you need to configure the iptables.
I got this script to help you:

#!/bin/sh
# ------------------------------------------------------------------------------------
# See URL: http://www.cyberciti.biz/tips/linux-setup-transparent-proxy-squid-howto.html
# (c) 2006, nixCraft under GNU/GPL v2.0+
# -------------------------------------------------------------------------------------
# squid server IP
SQUID_SERVER="192.168.1.1"
# Interface connected to Internet
INTERNET="eth0"
# Interface connected to LAN
LAN_IN="eth1"
# Squid port
SQUID_PORT="3128"
 
# DO NOT MODIFY BELOW
# Clean old firewall
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
# Load IPTABLES modules for NAT and IP conntrack support
modprobe ip_conntrack
modprobe ip_conntrack_ftp
# For win xp ftp client
#modprobe ip_nat_ftp
echo 1 > /proc/sys/net/ipv4/ip_forward
# Setting default filter policy
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
# Unlimited access to loop back
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# Allow UDP, DNS and Passive FTP
iptables -A INPUT -i $INTERNET -m state --state ESTABLISHED,RELATED -j ACCEPT
# set this system as a router for Rest of LAN
iptables --table nat --append POSTROUTING --out-interface $INTERNET -j MASQUERADE
iptables --append FORWARD --in-interface $LAN_IN -j ACCEPT
# unlimited access to LAN
iptables -A INPUT -i $LAN_IN -j ACCEPT
iptables -A OUTPUT -o $LAN_IN -j ACCEPT
# DNAT port 80 request comming from LAN systems to squid 3128 ($SQUID_PORT) aka transparent proxy
iptables -t nat -A PREROUTING -i $LAN_IN -p tcp --dport 80 -j DNAT --to $SQUID_SERVER:$SQUID_PORT
# if it is same system
iptables -t nat -A PREROUTING -i $INTERNET -p tcp --dport 80 -j REDIRECT --to-port $SQUID_PORT
# DROP everything and Log it
iptables -A INPUT -j LOG
iptables -A INPUT -j DROP

Okay, that’s all of it. If you like it please leave me a comment.

What people search:

Post comment as twitter logo facebook logo
Sort: Newest | Oldest

do you have dns server in your local network? if yes in your dns server (usually bind9) configure proxy.server.com to correct address of your proxy server.

imran 5 pts

how like to configure. i have install bind9 and configure this is cache server nothing else. i can access the sites through ip from client but can't through name.

please suggest|:

imran

shahmeer09@gmail.com

ivankrisdotcom 14 pts moderator

imran Yes it is dns problem. If you are not using squid, can you access the website with address name?

imran 5 pts

ivankrisdotcom ivan thanks for quick reply. i just stop the squid server then check the internet on the client machine. its not going to access the sites with proxy and without proxy. when i start the squid the client access the web through proxy setting. so please suggest.

I am running proxy server according to your configuration. I want to know what should I do to make my host name work instead of ip adress of the server to be place in the proxy.

my server name is:  proxy.server.com
Lan: IP address is 192.168.111.1

I used this IP addres (192.168.111.1 and port 3128 to connect to internet).
Now, I dont want to use the IP address. I want to use the name instead of IP address.

When I try to use the name on ther server the internet is working( proxy.server.com:3127).
But when I try to use the name on the client. It is not working. Why it is not working?

Hi Ivan ,

I am using squid 2.6 with no iptables but I can't able to access ftp and https connection
my proxy denied 443 and 21. I have done entry in Safe port and SSL port.

you need to use iptables.

Nice article,I was trying for installing, but could not , this would guide me for installing and configuring the squid.

Ok how about this scenario. I am using 80 as the squid http_port. So no need of redirection. And im not using iptables ( turned off ) at all for some reasons. How can I make my squid as transparent ? I need to redirect all http and https to be redirected to my squid server via ASA PIX 5510.

@8fa8bd4e70261688e1730c7b5e663f9d:disqus You still need iptables.

Hi above are the very good tips,
can you please suggest me that how to block the website in squid.

Regards
Jitender Saini

http://www.ivankristianto.com/os/ubuntu/howtofilter-some-website-during-work-hour-with-squid/915/

Hi i was wondering how i would go about configuring so that i can cap the amount of internet each proxy user can use, so say once a user has used 8GBs of cap, they're unable to access the internet anymore, unless their allocated amount of usage is increased

i thinks you need is not proxy/squid. try Mikrotik instead. It's a linux with lot of router application and feature.

hi ivan...

I was trying to configure my squid just like your configuration... but I was wondering why it is terminating automatically...

then I found out I had signal 6 error on messeges.log

Mar 22 18:16:19 localhost squid[1834]: Squid Parent: child process 1846 exited due to signal 6

then I tried to comment these:

#cache_mem 256 MB
#cache_dir ufs /var/spool/squid 2048 16 256
#cache_mgr admin@email.com
#cache_effective_user squid
#cache_effective_group squid

then it started normally... why do you think is happening? i would greatly appreciate it if you help me.. thanks...

what is your squid version.

thank bro,, you give one solution,, and in my main light out something,, and i get to try in my server,, my squid.conf is not valid,, and i try your configuration,, it so nice,,, thanks

Hi Ivan

I follow all steps one by one, but when I run your script i got the following error:

./iptables4squid.sh: 28: gt: not found
1
./iptables4squid.sh: 28: /proc/sys/net/ipv4/ip_forward: Permission denied

Could you give me a push please?

Regards

Christian

Hi Chrisitan,
Since WordPress automatically change special characters it turn > to "& g t;"
Please change it back to >

Thanks Ivan, this is very usefull script

it is really helpfull to me

hi ivan.
I have setup the squid and the iptables rules.
my eth0(192.168.1.24) is connected to internet and my eth1(192.168.10.0/24) is connected to the client.
the problem is that my client cannot access the internet.
im using squid 2.7 and ubuntu server 10.10

please make sure you install BIND and forward DNS to your ISP DNS.

Also i have some problem with iptables..
when i restart the server all the rules is set back to default...

Im kinda a noob..
I have an internal dns server(192.168.1.1)
so do i still need to install bind??
thx in advance..

You need to install Bind in your Router box, so when your client try to resolve the address it will forward to your ISP dns server. And yes, you need bind.

thx this really helps..
i fix the iptables problem by adding it manually to /etc/iptables.up.rules
i didnt use the terminal...

Hi Ivan, thanks for the tute, it worked great even in win xp. The only thing I cant find is where the iptables file is to copy the data into it. Would you know? I am looking in squid/etc but it is not there, I am assuming that it is a txt like the config file?

Thanks

Hi Ivan, couple of days back, i followed the article and installed and configure squid 2.6 and iptables on debian lenny, it worked fine for me. But now i installed Debian Squeeze in my test environment, that has squid 2.7, i configured it in same way as mentioned in this article but i am not able to ping to any site and not able to browse on my client system. What could be the reason? do i need to make any changes to the above article to work with Debian Squeeze?

Have you install BIND? And dns forwarding?

yes, i have local DNS installed and configured and also DNS forwarding is ON.

Have you check the iptables rules setup? and please see squid.log if there is any error.

Hi Ivan,
Nice Article !!!
Will it work with dansguadian?
Thanks
Boss

Hi Boss,
to have it works with dansguardian, you need to confugre squid and iptables.

Thanks for the update Ivan.
could you please update the configurations for both squid and iptables.

Please update only the iptables configuration. i configured the squid + dansguardian
Thanks in Advance!

then block proxy.skyinet.net so they cannot access it.

Hi Ivan.. Im Jake and i am a newbe in unix/linux world i have setup a FreeBSD tranparent proxy/cache server and it work fine.. my question is can i use squid to filter p2p download? and how do manage to restrict Pornsites in my Network? Thanks..

to restrict p2p is little bit tricky. You may need to block all port except the default port (ex: 80, 21, 22, etc). But it will block passive ftp or other program port as well. So you need extra attention to what port you really need to open.

Thanks Ivan,, 1 more thing, how can i force my client to use only my proxy server.. even if they manually configure thier browser to use proxy.skyinet.net they cannot access to web.

as of now when i block some site to my squid they cannot access the site but when they used the proxy.skyinet.net they can access my blocklist sites.. can you please help?

Ivan,
Here is my setup. DSLRouter>--<eth0.DebianLenny.eth1>--<WirelessNetgear>--<LaptopViaWireless>

i got squid and iptables to work. i see lthe access logs in squid. That part is cool. I assigned eth1 192.168.13.1 and Netgear WAN 192.168.13.2 connected to eth1 with direct cable. LAN (wireless) on netgear is 192.168.16.1 and disctributes 192.168.16.100 to my laptop connected via wireless. i only see the squid access.log file with router ip 192.168.13.2 and not the laptop ip 192.168.16.100. how to fix that ? Thanks in advance for your help. or do i need to re-arrange my network or some squid changes are required ?

since you are using a router (wireless router), all connection from the router will pass to use the same IP address. You need to enable One-To-One NAT in your wireless router. So your laptop ip 192.168.16.100 will translate to 192.168.13.102. For setting in your wireless router please refer to your product manual.

Thanks for the reply, so there are no settings on squid we can accomplish this with ?

As far as i know, there is no you can do with squid. because squid will only see the incoming ip connection which is your router ip. So the only way is setting one-to-one NAT. Anyway if you have better solution please let me know.

I am also having the permission denied error when I'm trying to execute the script. I saw you wrote to some guy to change the ">" to > without the quotes...uh where is that?

to give execute permission to script type this: chmod +x iptables.sh and then run it with ./iptables.sh

yes I did that and I still get the permission denied...

can you paste what is the result of this command?
ls -l iptables.sh

Hi Ivan
I didnt check the bind and dns forwarding let me try

Thanks

Nassar