To set up Squid on CentOS, install the package from the default repositories, edit the /etc/squid/squid.conf file to define cache directories and access controls, then start the service and enable it at boot.
Background and Purpose of Squid on CentOS
Squid is a widely used caching proxy that improves web performance, reduces bandwidth usage, and provides access control for networked clients. On CentOS, Squid can be deployed as a forward proxy for internal users, a reverse proxy in front of web servers, or a transparent intercepting proxy that redirects traffic without client configuration. The CentOS distribution ships Squid in its default package repositories, making installation straightforward and ensuring that the binary is compiled against the same libraries used by the system.
Prerequisites and System Preparation
Before installing, confirm that your system is up to date and that you have a non‑root user with sudo privileges. Squid requires a dedicated cache directory; by default it uses /var/spool/squid, but you can choose a larger or faster disk if your traffic volume is high.
- Update the package index:
sudo dnf update -y - Create a dedicated cache user (optional but recommended):
sudo useradd -r -s /sbin/nologin squid - Ensure the cache directory exists and has proper ownership:
sudo mkdir -p /var/spool/squidandsudo chown squid:squid /var/spool/squid - Verify that the firewall allows the ports you plan to use (usually 3128 for HTTP).
Installing Squid on CentOS
- Install the Squid package:
sudo dnf install squid -y - After installation, the service file
/usr/lib/systemd/system/squid.serviceis automatically created. Verify its status withsystemctl status squid. - Enable the service to start on boot:
sudo systemctl enable squid - Start the service:
sudo systemctl start squid - Check that Squid is listening on the default port:
ss -tlnp | grep 3128. You should see the Squid process bound to0.0.0.0:3128or the specific interface you configured.
Basic Configuration for a Transparent Proxy
To redirect traffic without client changes, you need to modify both Squid’s configuration and the host’s firewall or routing rules. The following steps illustrate a common transparent setup on CentOS 8/9.
- Edit
/etc/squid/squid.confand set the port to 3128 (or another unused port). Add the following line if you want to use a different port:http_port 3128 transparent - Define ACLs (Access Control Lists) to allow internal networks. For example, to allow the 192.168.1.0/24 subnet:
acl localnet src 192.168.1.0/24 http_access allow localnet http_access deny all - Set the cache directory size and location:
This reserves 100 GB of space, with 16 kB block size and 256 subdirectories.cache_dir ufs /var/spool/squid 100 16 256 - Restart Squid to apply changes:
sudo systemctl restart squid - Configure the firewall to forward HTTP traffic to the Squid port. On CentOS, use firewalld:
sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=3128 sudo firewall-cmd --reload - Adjust the host’s routing or NAT rules (iptables or nftables) to redirect outbound traffic to port 3128. An example iptables rule:
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3128
After these steps, any HTTP request from the 192.168.1.0/24 network will be intercepted by Squid, cached, and served from the proxy.
Fine‑Tuning Performance and Security
Once the basic proxy is operational, you can adjust several parameters to improve throughput and protect the server.
- Cache tuning – Increase
maximum_object_size_in_memoryif you frequently cache large images, or reduce it to keep memory usage low. Setmaximum_object_sizeto limit how large an object can be cached on disk. - Connection limits – The
max_object_file_sizeandmax_requests_per_childdirectives control how many connections a single Squid process can handle. For high‑traffic environments, increasemax_requests_per_childto 5000 or more. - SSL/TLS support – To handle HTTPS traffic, install the
squid-ssl-bumppackage and configurehttps_portwith a self‑signed certificate. This allows the proxy to inspect encrypted traffic. - Logging and monitoring – Enable
access_logandcache_logto capture detailed usage statistics. Use tools likesquidclientorsquid-analyzerto parse logs. - Security hardening – Disable the
visible_hostnameif you do not want the proxy to expose its name. Usehttp_port 3128withhttp_access deny allafter your allow rules to block unknown traffic. Keep the Squid package up to date to receive security patches.
Troubleshooting Common Issues
Even with careful configuration, problems can arise. The following checklist helps diagnose typical problems.
- Service not starting – Check
journalctl -u squidfor errors. Common causes include missing/var/spool/squidpermissions or syntax errors insquid.conf. - Clients cannot connect – Verify that the firewall allows port 3128 and that NAT rules correctly redirect traffic. Use
tcpdump -i eth0 port 3128to confirm packets reach Squid. - Cache not filling – Ensure
cache_dirhas enough free space and that thecache_memsetting is appropriate. Checkcache.logfor “disk full” messages. - HTTPS not working – If you enabled SSL bumping, confirm the certificate is trusted by clients and that
http_port 3128 ssl-bumpis correctly configured. Look for “SSL alert” entries in the logs. - High memory usage – Reduce
maximum_object_size_in_memoryor lowercache_memto limit RAM consumption. Monitor withtoporhtop.
Alternatives and When to Use Them
If Squid does not meet your needs, consider these alternatives:
- Nginx as a reverse proxy – Ideal for HTTP/HTTPS caching in front of web applications, with built‑in load balancing.
- HAProxy – Offers advanced TCP/HTTP load balancing and can cache static content with the
cachemodule. - Varnish – A high‑performance HTTP accelerator that excels at caching dynamic content when combined with a backend server.
- Privoxy – A lightweight filtering proxy that focuses on privacy and ad blocking rather than caching.
Frequently Asked Questions
How do I enable HTTPS proxying with Squid?
Install the squid-ssl-bump package, generate a self‑signed certificate, and add an https_port directive with the ssl-bump option. Clients must trust the certificate, otherwise browsers will warn about insecure connections.
Can I use Squid as a reverse proxy for my web server?
Yes. Configure http_port to listen on the desired port, then add cache_peer directives pointing to your backend server. Set never_direct to force Squid to forward requests to the peer.
What is the default cache directory on CentOS?
By default, Squid uses /var/spool/squid. The directory is created during installation, but you can change it in squid.conf with the cache_dir directive.
How do I restrict access to Squid to specific IP ranges?
Use ACLs in squid.conf. For example, acl allowed src 10.0.0.0/8 followed by http_access allow allowed and http_access deny all ensures only the specified subnet can use the proxy.
Is Squid safe to run as root?
No. Squid runs as the squid user by default. Running it as root can expose the system to privilege escalation if a vulnerability is exploited.