Setup Squid proxy with caching on CentOS
Squid Proxy setup with caching on CentOS is our next short tutorial where we will learn how to install Squid in just a few easy steps, with caching enabled on a CentOS VM. Squid is still one of the best software for setting up a proxy that comes packed with many great features like caching, authentication and many others despite the fact that it has a very small footprint in terms of resources compared to many other solutions available on the market. By using Squid’s caching feature we can save a lot of bandwidth and we can even reduce the costs if for example this solution is used in a cloud environment. In this particular tutorial we will show you how easy is to setup Squid on CentOS, enable caching and also how to configure all Squid clients to use a system-wide proxy solution based on Squid, basically all external requests from these clients will be passed to Squid and from here we can easily manage these in various ways to fit to our scenario. This means that we don’t necessarily have to use any managed or unmanaged NAT solution to pull data from the internet unless we have a good reason for that. By using Squid we can decrease and even eliminate NAT Gateways costs by simply using its proxy and caching features, this being also a more elegant and flexible solution as opposed to any managed solution from any cloud provider.
Table of contents
Setup Squid on CentOS using yum
Configure Squid caching path
Enable and start Squid service
Configure system-wide proxy for Squid clients
Setup Squid on CentOS using yum
Setting up Squid on CentOS is not different than any other base package that comes within CentOS default repos, all we have to do is just to use yum
package manager to install this on our system, lets do a system update first just to make sure that we are using latest updates, assuming that we have a dedicated VM machine for setting up squid on it:
yum update
Once our system is up to date then we can safely proceed to install Squid like shown in the example below, one again using yum
, so lets type the next command on our terminal window:
yum install -y squid
We are now done installing, this is all we have to install on our machine in terms of packages in order to setup a proxy server, we can now move to our next step where we have to configure Squid’s caching path.
Configure Squid caching path
This particular step is not quite a must but as we have said in the beginning of our tutorial this can really have a positive impact on cost savings from a bandwidth point of view. Using Squid’s Caching feature it means that all clients will pick up data from the internet like packages for example only when these are not present on Squid machine. For example a simple yum update
from one of the clients configured to use Squid as proxy server will store all received data to Squid first, the second machine that will invoke later yum update
will pick up the files straight from Squid machine as these are already present there.
Prior configuring Squid Caching please make sure that you have a dedicated disk for this on you Squid machine, it doesn’t necessarily have to be a fast disk. Once the disk is in place lets start by editing squid.conf
file like shown here:
vi /etc/squid/squid.conf
Assuming that we have a 20GB disk mounted on /mnt/squid/cache
, specifically for Squid Caching lets change cache_dir
parameters like this:
cache_dir ufs /mnt/squid/cache 20000 16 256
Above we have four main parameters needed for caching, /mnt/squid/cache
is the path where all cached content will be placed, 20000
(20GB) is the overall maximum cache size that can be stored by Squid, 16
is the first-level subdirectories for all cached content – root folders under caching path – and finally 256
is the second-level subdirectories. Don’t go wild and increase the values for first and second-level subdirectories because then Squid will spend a lot of time browsing these subdirectories instead of serving files.
Now we can save and close squid.conf
file and lets make sure that /mnt/squid/cache
is owned by squid
user using chown
command like this:
chown -R /mnt/squid/cache squid:squid
Enable and start Squid service
This is the most easy part of our entire tutorial, enabling Squid service daemon and starting the service:
systemctl enable squid && systemctl start squid
These are all the septs needed for setting up our Squid proxy and caching solution, now we will be moving to the next step where we have to configure the clients.
Configure system-wide proxy for Squid clients
Now on each host client (VM, bare metal etc.) let’s edit the environment
file, this particular file helps us to implement our Squid system-wide proxy solution:
vi /etc/environment
All we have to do is just to add the next three lines, of course assuming that our Squid proxy server has the IP address 10.10.0.2
:
http_proxy="http://10.10.0.2:3128"
https_proxy="https://10.10.0.2:3128"
ftp_proxy="ftp://10.10.0.2:3128"
Save the file and enjoy, that is all, our short tutorial ends here.