Creating an HTTP proxy using Squid on CentOS allows you to intercept, cache, and filter web traffic between a client and the internet. This setup is primarily used to improve network performance by caching frequently accessed content, enhancing security by blocking specific domains, and masking the internal IP addresses of a local network. By routing requests through a central Squid server, administrators gain granular control over how users access the web and can implement bandwidth management policies to prevent a single user from consuming all available network resources.

What You'll Need

  • A server running CentOS (version 7, 8, or Stream) with root or sudo privileges.
  • A static IP address assigned to the CentOS server to ensure clients can consistently connect to the proxy.
  • An active internet connection on the server to download the necessary packages.
  • Basic familiarity with the Linux command line and text editors like Vim or Nano.
  • Access to the system firewall (firewalld) to open the required proxy ports.

Step-by-Step

  1. Update the System: Before installing new software, ensure your package repository is up to date to avoid dependency conflicts. Run the command sudo yum update -y (or dnf update -y on CentOS 8/Stream). This ensures that the latest security patches and stable versions of libraries are present on the system.
  2. Install the EPEL Repository: Squid is not always available in the default CentOS base repositories. You must install the Extra Packages for Enterprise Linux (EPEL) repository to access it. Execute sudo yum install epel-release -y. The EPEL repository is a community-driven project that provides high-quality add-on packages for RHEL-based distributions.
  3. Install Squid: Once the EPEL repository is enabled, install the Squid package using the command sudo yum install squid -y. This process downloads the Squid binary along with all necessary dependencies required for the proxy to handle HTTP and HTTPS traffic.
  4. Configure the Squid Configuration File: The main configuration file is located at /etc/squid/squid.conf. Because this file is extensive and contains many default comments, it is highly recommended to create a backup before making changes: sudo cp /etc/squid/squid.conf /etc/squid/squid.conf.bak. Open the file with a text editor: sudo vi /etc/squid/squid.conf.
  5. Define Access Control Lists (ACLs): By default, Squid denies all incoming traffic for security reasons. You must define who is allowed to use the proxy. Find the section for ACLs and add a line to define your local network. For example, if your clients are on the 192.168.1.0/24 subnet, add: acl localnet src 192.168.1.0/24. This tells Squid that any IP address within this range is considered part of the trusted local network.
  6. Grant Access to the ACL: Defining the ACL is not enough; you must explicitly allow it. Look for the line http_access deny all. Above that line, add http_access allow localnet. The order of these lines is critical because Squid processes rules from top to bottom. If deny all comes first, your local network will be blocked regardless of the subsequent allow rule.
  7. Change the Default Port (Optional): Squid defaults to port 3128. If you wish to change this for security or organizational reasons, find the line http_port 3128 and change it to your preferred port, such as http_port 8080. Ensure you remember this port for the firewall and client configuration steps.
  8. Configure Caching Settings: To optimize performance, adjust the cache size. Find the cache_dir ufs /var/spool/squid 100 16 256 directive. The '100' represents the disk space in megabytes. Increase this value based on your available disk space to store more web content locally, reducing external bandwidth usage.
  9. Start and Enable the Squid Service: Once the configuration is saved, start the service using sudo systemctl start squid. To ensure the proxy starts automatically after a server reboot, run sudo systemctl enable squid. You can verify the status of the service with sudo systemctl status squid.
  10. Configure the Firewall: CentOS uses firewalld by default, which will block incoming connections to the proxy port. Open the port using the following commands: sudo firewall-cmd --permanent --add-port=3128/tcp (replace 3128 if you changed the port). Then, apply the changes with sudo firewall-cmd --reload.
  11. Test the Proxy on a Client: On a client machine (Windows, macOS, or Linux), go to the network settings and enter the IP address of your CentOS server and the port (3128). Attempt to browse a website. If the page loads, the proxy is functioning correctly. You can check the server logs in real-time using sudo tail -f /var/log/squid/access.log to see the incoming requests.

Common Mistakes

One of the most frequent errors is the misplacement of the http_access rules. Because Squid evaluates rules sequentially, placing a general deny all rule before a specific allow localnet rule results in a "Access Denied" error for all users. Always place your specific permissions at the top of the access list. Another common mistake is forgetting to restart the Squid service after modifying squid.conf. Changes to the configuration file are not applied dynamically; the service must be restarted or reloaded using sudo systemctl reload squid for the new settings to take effect. Users also frequently overlook the SELinux settings on CentOS. SELinux can prevent Squid from binding to a non-standard port. If you change the port from 3128 to something else and the service fails to start, you may need to update the SELinux policy using semanage port -a -t squid_port_t -p tcp [PORT_NUMBER]. Finally, failing to configure the firewall is a primary cause of connectivity issues. Even if the service is running perfectly on the server, the client will receive a "Connection Timed Out" error if the CentOS firewall is still blocking traffic on port 3128.

Tips

To enhance the security of your proxy, implement basic authentication. This prevents unauthorized users from using your server as an open proxy, which could lead to your server IP being blacklisted. You can use the htpasswd utility from the httpd-tools package to create a password file and then reference it in squid.conf using the auth_param basic directive. For better performance, consider tuning the memory cache. The cache_mem directive allows you to specify how much RAM Squid should use to store objects. Increasing this value on servers with ample RAM can significantly speed up response times for frequently accessed small files. If you are managing a large number of blocked sites, do not list them individually in the main config file. Instead, create a separate text file (e.g., /etc/squid/blocked_sites.txt) and reference it in the configuration using acl blocked_sites dstdomain "/etc/squid/blocked_sites.txt" followed by http_access deny blocked_sites. This keeps your main configuration clean and easier to maintain. Monitoring your proxy traffic is essential for identifying bottlenecks. Use the squidclient tool to query the manager for statistics on cache hit rates and current active connections, which helps in deciding when to scale your hardware resources.

Troubleshooting

If the proxy is not working, the first place to look is the access log located at /var/log/squid/access.log. If you see TCP_DENIED/403, it means the request reached the server but was rejected by your ACL rules. Re-examine your http_access sequence and ensure the client's IP address falls within the defined localnet range. If the client cannot connect at all (Connection Refused), check if the Squid service is actually running with systemctl status squid. If the service is inactive, check the system logs using journalctl -u squid to find syntax errors in the configuration file. A single missing character or typo in squid.conf can prevent the service from starting. If the service is running but the client still cannot connect, test the port locally on the server using telnet localhost 3128. If it connects locally but not from the client, the issue is almost certainly the firewall. Verify the open ports with sudo firewall-cmd --list-ports. For issues involving HTTPS traffic, remember that Squid acts as a standard HTTP proxy. For HTTPS, it uses the CONNECT method to create a tunnel. If you are trying to inspect or filter HTTPS content (SSL Bumping), you will need to generate a self-signed CA certificate and install it on all client machines; otherwise, the clients will receive security warnings because the proxy is intercepting the encrypted stream.

Frequently Asked Questions

Can Squid be used as a transparent proxy?

Yes, Squid can be configured as a transparent proxy, meaning clients do not need to be manually configured to use it. This requires advanced network routing (usually via iptables) to redirect traffic from port 80 to the Squid port.

Does Squid support HTTPS filtering?

By default, Squid tunnels HTTPS traffic without seeing the content. To filter or cache HTTPS content, you must implement "SSL Bump," which involves decrypting the traffic, inspecting it, and re-encrypting it before sending it to the client.

How do I block specific websites using Squid?

You can block websites by creating a destination domain ACL. Add the domains to a list file and then add an http_access deny rule for that ACL in the configuration file.

Will Squid slow down my internet connection?

Initially, there may be a slight overhead. However, once the cache is populated, frequently accessed pages will load significantly faster because they are served from the local disk rather than the remote server.

How can I limit bandwidth for specific users?

Squid provides "delay pools" that allow you to limit the maximum bandwidth available to specific ACLs, ensuring that no single user saturates the network connection.

Is Squid compatible with CentOS Stream?

Yes, Squid is fully compatible with CentOS Stream. The installation and configuration process remains identical to CentOS 7 or 8, utilizing the EPEL repository for the package.