Vous êtes sur la page 1sur 13

Squid proxy :

Squid is a proxy server for caching and filtering web content . Squid proxy is used by various
organisation and internet providers to reduce bandwidth and to increase response time .
Squid proxy service will cache the requested web-content and re-using it for the further request
of the same content .

In the above diagram, squid proxy will cache the webcontent of krizna.com from the ISP during
the first request and it will deliver the cached content for the further requests of krizna.com
without requesting from ISP. This will reduce bandwidth and will increase response time as the
content is delivered from the local network.

In this article we can see how to install and configure squid proxy on centos 6 . After installing
Centos 6 (Centos 6 installation step by step screenshots ) and configuring network (How to setup
network in centos 6)
Update yum repositories and packages by typing the below command
[root@leela ~]# yum update

Step 1 Install squid package and dependencies using the below command
[root@leela ~]# yum install squid
By default squid configuration file /etc/squid/squid.conf will contains recommended
minimum configuration and squid caching feature will work without making any changes .
Recommended minimum configuration will looks like the below

# vi /etc/squid/squid.conf
1 #
2 # Recommended minimum configuration:
3 #
4 acl manager proto cache_object
5 acl localhost src 127.0.0.1/32 ::1
6 acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
7
8 # Example rule allowing access from your local networks.
9 # Adapt to list your (internal) IP networks from where browsing
10 # should be allowed
11 acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
12 acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
13 acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
14 acl localnet src fc00::/7
# RFC 4193 local private network range
15 acl localnet src fe80::/10
# RFC 4291 link-local (directly plugged) machines
16
17 acl SSL_ports port 443
18 acl Safe_ports port 80
# http
19 acl Safe_ports port 21
# ftp
20 acl Safe_ports port 443
# https
21 acl Safe_ports port 70
# gopher
22 acl Safe_ports port 210
# wais
23 acl Safe_ports port 1025-65535 # unregistered ports
24 acl Safe_ports port 280
# http-mgmt
25 acl Safe_ports port 488
# gss-http
26 acl Safe_ports port 591
# filemaker
27 acl Safe_ports port 777
# multiling http
28 acl CONNECT method CONNECT

29
30 #
31 # Recommended minimum Access Permission configuration:
32 #
33 # Only allow cachemgr access from localhost
34 http_access allow manager localhost
35 http_access deny manager
36
37 # Deny requests to certain unsafe ports
38 http_access deny !Safe_ports
39
40 # Deny CONNECT to other than secure SSL ports
41 http_access deny CONNECT !SSL_ports
42
43 # We strongly recommend the following be uncommented to protect innocent
44 # web applications running on the proxy server who think the only
45 # one who can access services on "localhost" is a local user
46 #http_access deny to_localhost
47
48 #
49 # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
50 #
51 # Example rule allowing access from your local networks.
52 # Adapt localnet in the ACL section to list your (internal) IP networks
53 # from where browsing should be allowed
54 http_access allow localnet
55 http_access allow localhost
56
57 # And finally deny all other access to this proxy
58 http_access deny all
59
60 # Squid normally listens to port 3128
61 http_port 3128
62
63 # We recommend you to use at least the following line.
64 hierarchy_stoplist cgi-bin ?
65
66 # Uncomment and adjust the following to add a disk cache directory.
67 #cache_dir ufs /var/spool/squid 100 16 256
68
69 # Leave coredumps in the first cache dir
70 coredump_dir /var/spool/squid
71
72 # Add any of your own refresh_pattern entries above these.
73 refresh_pattern ^ftp:
1440 20% 10080
74 refresh_pattern ^gopher:
1440 0%
1440

75 refresh_pattern -i (/cgi-bin/|\?) 0 0%
0
76 refresh_pattern .
0
20% 4320
Step 2 Now start squid service
[root@leela ~]# service squid start
and type this below command to start squid service automatically while booting.
[root@leela ~]# chkconfig --levels 235 squid on
Step 3 Setup your web browser to access Internet through proxy server on port 3128 .
IE : Tools Internet options Connections LAN settings Choose Use a proxy server for
your LAN Type your Proxy server ip (192.168.1.11 ) and port no 3128
Firefox : Options / Preferences Advanced Network Settings Choose Manual proxy
configuration Type your Proxy server ip (192.168.1.11 ) and port no 3128
Step 4 Browse some sites and check the access log file on proxy server

1 [root@leela ~]# cat /var/log/squid/access.log


2 1343759484.331 1828 192.168.1.15 TCP_MISS/200 7005 GET http://www.krizna.com/ 3 DIRECT/216.172.163.231 text/html
4 1343759484.645 265 192.168.1.15 TCP_MISS/304 477 GET
5 http://platform.twitter.com/widgets.js - DIRECT/23.64.79.144 application/javascript
6 1343759484.681 309 192.168.1.15 TCP_MISS/304 839 GET
7 http://apis.google.com/js/plusone.js - DIRECT/173.194.36.36 8 1343759485.005 178 192.168.1.15 TCP_MISS/200 495 GET http://www.google9 analytics.com/__utm.gif? - DIRECT/74.125.236.142 image/gif
10 1343759502.763 20292 192.168.1.15 TCP_MISS/200 1898 CONNECT s11 static.ak.facebook.com:443 - DIRECT/23.64.66.110 12 1343759502.765 20292 192.168.1.15 TCP_MISS/200 1898 CONNECT s13 static.ak.facebook.com:443 - DIRECT/23.64.66.110 14 1343759723.040 240567 192.168.1.15 TCP_MISS/200 2904 CONNECT
15 plusone.google.com:443 - DIRECT/173.194.36.41 1343759933.642 958 192.168.1.15 TCP_MISS/304 394 GET http://www.krizna.com/ DIRECT/216.172.163.231 -

1343759934.518 459 192.168.1.15 TCP_MISS/200 495 GET http://www.googleanalytics.com/__utm.gif? - DIRECT/173.194.36.8 image/gif


1343759943.131 2186 192.168.1.15 TCP_MISS/200 8343 CONNECT
www.facebook.com:443 - DIRECT/66.220.146.94 1343759943.266 10616 192.168.1.15 TCP_MISS/200 2790 CONNECT
apis.google.com:443 - DIRECT/74.125.236.128 1343759943.267 10615 192.168.1.15 TCP_MISS/200 2228 CONNECT ssl.gstatic.com:443
- DIRECT/74.125.236.143 1343759950.875 2596 192.168.1.15 TCP_MISS/200 32219 CONNECT
www.facebook.com:443 - DIRECT/66.220.146.94 1343759961.388 20443 192.168.1.15 TCP_MISS/200 1911 CONNECT sstatic.ak.fbcdn.net:443 - DIRECT/23.64.77.177 Troubleshooting :
If you not able to browse using proxy settings , Disable the firewall ( iptables ) and selinux
service on your squid proxy server .
Disable firewall ( Iptables )
[root@leela ~]# service iptables stop
[root@leela ~]# chkconfig iptables off
Disable Selinux open the file /etc/selinux/config and find the line
SELINUX=enforcing
and replace with
SELINUX=disabled
now reboot the server and try step 4.
Configure squid proxy as web filter:
You can restrict user access to particular websites or keywords using access control lists (ACLs) .
Restricting Access to specific web sites :

For example , we can see how to block facebook.com and gmail.com .


Step 1 create a file ( /etc/squid/blockedsites.squid ) and add the site names one per line.
[root@leela ~]# vi /etc/squid/blockedsites.squid
#blocked sites
www.youtube.com

Step 2 Open the /etc/squid/squid.conf and create a new acl blocksites and acl type
dstdomain in the acl section like the below .
# vi /etc/squid/squid.conf

1 acl Safe_ports port 488


# gss-http
2 acl Safe_ports port 591
# filemaker
3 acl Safe_ports port 777
# multiling http
4 acl CONNECT method CONNECT
5 # ACL blocksites
6 acl blocksites dstdomain "/etc/squid/blockedsites.squid"
and add the following line http_access deny blocksites to http_section to deny the access to the
acl blocksites .

1 # Recommended minimum Access Permission configuration:


2#
3 # Only allow cachemgr access from localhost
4 http_access allow manager localhost
5 # Deny access to blocksites ACL
6 http_access deny blocksites
Step 3 Now restart squid service

[root@leela ~]# service squid restart


Step 4 Try to access facebook.com in your browser . you could see the blocked page as below .

and check the log file you can see the facebook request is denied .

[root@leela ~]# tail -f /var/log/squid/access.log


.................................................................................
1
1343820985.542
1 192.168.1.15 TCP_DENIED/403 4255 GET http://www.facebook.com/
2
- NONE/- text/html
3
1343820986.998 1411 192.168.1.15 TCP_MISS/200 13152 GET http://www.squid4
cache.org/Artwork/SN.png - DIRECT/198.186.193.234 image/png
5
1343820987.004
0 192.168.1.15 TCP_DENIED/403 4178 GET
http://www.facebook.com/favicon.ico - NONE/- text/html
Restricting Access to specific keywords :

Step 1 create a file ( /etc/squid/blockkeywords.squid ) and add the keywords one per line.
[root@leela ~]# cat /etc/squid/blockkeywords.squid
#blocked keywords
sex
porn
xxx
Step 2 Open the /etc/squid/squid.conf and create a new acl blockkeywords and acl type
url_regex in the acl section

1 acl Safe_ports port 488


# gss-http
2 acl Safe_ports port 591
# filemaker
3 acl Safe_ports port 777
# multiling http
4 acl CONNECT method CONNECT
5 # ACL blocksites
6 acl blocksites dstdomain "/etc/squid/blockedsites.squid"
7 # ACL blockkeywords
8 acl blockkeywords url_regex -i "/etc/squid/blockkeywords.squid"
and add the following line http_access deny blockkeywords to http_section to deny the access
to the acl blockkeywords .

1 # Recommended minimum Access Permission configuration:


2#
3 # Only allow cachemgr access from localhost
4 http_access allow manager localhost
5 # Deny access to blocksites ACL
6 http_access deny blocksites
7 # Deny access to blockkeywords ACL
8 http_access deny blockkeywords
Restricting Access to specific Ipaddress :
Step 1 create a file ( /etc/squid/blockip.squid ) and add the ip adresses one per line.
[root@leela ~]# cat /etc/squid/blockip.squid

#blocked ips
192.168.1.20
192.168.1.21
Step 2 Open the /etc/squid/squid.conf and create a new acl blockip and acl type src in the
acl section

1 acl Safe_ports port 488


# gss-http
2 acl Safe_ports port 591
# filemaker
3 acl Safe_ports port 777
# multiling http
4 acl CONNECT method CONNECT
5 # ACL blocksites
6 acl blocksites dstdomain "/etc/squid/blockedsites.squid"
7 # ACL blockkeywords
8 acl blockkeywords url_regex -i "/etc/squid/blockkeywords.squid"
9 # ACL blockip
10 acl blockip src "/etc/squid/blockip.squid"
and add the following line http_access deny blockip to http_section to deny the access to the
acl blockip .

1 # Recommended minimum Access Permission configuration:


2 #
3 # Only allow cachemgr access from localhost
4 http_access allow manager localhost
5 # Deny access to blockip ACL
6 http_access deny blockip
7 # Deny access to blocksites ACL
8 http_access deny blocksites
9 # Deny access to blockkeywords ACL
10 http_access deny blockkeywords
Allow Full access to specific Ipaddress :
You can allow specific ip address to gain full access without blocking sites and keywords . just
create a file /etc/squid/allowip.squid and add the ip address one per line and create an acl
allowip and acl type src in the acl section

1 # ACL allowip
2 acl allowip src "/etc/squid/allowip.squid"
and add the allowip in the http_access as below

1 # Recommended minimum Access Permission configuration:


2 #
3 # Only allow cachemgr access from localhost
4 http_access allow manager localhost
5 # Deny access to blockip ACL
6 http_access deny blockip
7 # Deny access to blocksites ACL
8 http_access deny blocksites !allowip
9 # Deny access to blockkeywords ACL
10 http_access deny blockkeywords !allowip
Changing squid proxy port number:
You can change squid proxy port number , by default it uses 3128 port .
Just find the below line in /etc/squid/squid.conf
http_port 3128
and replace with
http_port 8000 # whatever port number you want
Restricting Download size :
You can restrict download file size using reply_body_max_size .
Add the below line at the bottom of the http_access section
#Restrict download size
reply_body_max_size 10 MB all

or
#Restrict download size
reply_body_max_size 10 MB !allowip
Configuring Squid as Transparent Proxy :
You can configure squid as transparent proxy .
Step 1 just find the below line
# Squid normally listens to port 3128
http_port 3128
and replace with
# Squid normally listens to port 3128
http_port 3128 intercept
Step 2 Create a file /root/squidfw.sh and add the following firewall script ( modify as per
your interface configuration ).

1 #!/bin/sh
2 # squid server IP
3 SQUID_SERVER="192.168.1.11"
4 # Interface connected to Internet
5 INTERNET="eth0"
6 # Interface connected to LAN
7 LAN_IN="eth1"
8 # Squid port
9 SQUID_PORT="3128"
10 # DO NOT MODIFY BELOW
11 # Clean old firewall
12 iptables -F
13 iptables -X
14 iptables -t nat -F
15 iptables -t nat -X

iptables -t mangle -F
iptables -t mangle -X
16 # Load IPTABLES modules for NAT and IP conntrack support
17 modprobe ip_conntrack
18 modprobe ip_conntrack_ftp
19 # For win xp ftp client
20 #modprobe ip_nat_ftp
21 echo 1 > /proc/sys/net/ipv4/ip_forward
22 # Setting default filter policy
23 iptables -P INPUT DROP
24 iptables -P OUTPUT ACCEPT
25 # Unlimited access to loop back
26 iptables -A INPUT -i lo -j ACCEPT
27 iptables -A OUTPUT -o lo -j ACCEPT
28 # Allow UDP, DNS and Passive FTP
29 iptables -A INPUT -i $INTERNET -m state --state ESTABLISHED,RELATED -j ACCEPT
30 # set this system as a router for Rest of LAN
31 iptables --table nat --append POSTROUTING --out-interface $INTERNET -j
32 MASQUERADE
33 iptables --append FORWARD --in-interface $LAN_IN -j ACCEPT
34 # unlimited access to LAN
35 iptables -A INPUT -i $LAN_IN -j ACCEPT
36 iptables -A OUTPUT -o $LAN_IN -j ACCEPT
37 # DNAT port 80 request comming from LAN systems to squid 3128 ($SQUID_PORT) aka
38 transparent proxy
39 iptables -t nat -A PREROUTING -i $LAN_IN -p tcp --dport 80 -j DNAT --to
40 $SQUID_SERVER:$SQUID_PORT
41 # if it is same system
42 iptables -t nat -A PREROUTING -i $INTERNET -p tcp --dport 80 -j REDIRECT --to-port
43 $SQUID_PORT
44 # DROP everything and Log it
iptables -A INPUT -j LOG
iptables -A INPUT -j DROP
Step 3 just run the script
[root@leela ~]# sh /root/squidfw.sh
and add the below line to /etc/rc.local to run the script during startup
sh /root/squidfw.sh
Step 4 (Updated) Change default gateway ip to squid server ip on the user machines .
Now users can access Internet without setting proxy in the browser settings.

Thats it , hope this article will help you to learn little things about configuring squid proxy on
centos 6.

Vous aimerez peut-être aussi