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/squid and sudo 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

  1. Install the Squid package: sudo dnf install squid -y
  2. After installation, the service file /usr/lib/systemd/system/squid.service is automatically created. Verify its status with systemctl status squid.
  3. Enable the service to start on boot: sudo systemctl enable squid
  4. Start the service: sudo systemctl start squid
  5. Check that Squid is listening on the default port: ss -tlnp | grep 3128. You should see the Squid process bound to 0.0.0.0:3128 or 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.

  1. Edit /etc/squid/squid.conf and 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
  2. 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
  3. Set the cache directory size and location:
    cache_dir ufs /var/spool/squid 100 16 256
    This reserves 100 GB of space, with 16 kB block size and 256 subdirectories.
  4. Restart Squid to apply changes: sudo systemctl restart squid
  5. 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
  6. 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_memory if you frequently cache large images, or reduce it to keep memory usage low. Set maximum_object_size to limit how large an object can be cached on disk.
  • Connection limits – The max_object_file_size and max_requests_per_child directives control how many connections a single Squid process can handle. For high‑traffic environments, increase max_requests_per_child to 5000 or more.
  • SSL/TLS support – To handle HTTPS traffic, install the squid-ssl-bump package and configure https_port with a self‑signed certificate. This allows the proxy to inspect encrypted traffic.
  • Logging and monitoring – Enable access_log and cache_log to capture detailed usage statistics. Use tools like squidclient or squid-analyzer to parse logs.
  • Security hardening – Disable the visible_hostname if you do not want the proxy to expose its name. Use http_port 3128 with http_access deny all after 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 squid for errors. Common causes include missing /var/spool/squid permissions or syntax errors in squid.conf.
  • Clients cannot connect – Verify that the firewall allows port 3128 and that NAT rules correctly redirect traffic. Use tcpdump -i eth0 port 3128 to confirm packets reach Squid.
  • Cache not filling – Ensure cache_dir has enough free space and that the cache_mem setting is appropriate. Check cache.log for “disk full” messages.
  • HTTPS not working – If you enabled SSL bumping, confirm the certificate is trusted by clients and that http_port 3128 ssl-bump is correctly configured. Look for “SSL alert” entries in the logs.
  • High memory usage – Reduce maximum_object_size_in_memory or lower cache_mem to limit RAM consumption. Monitor with top or htop.

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 cache module.
  • 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.