Chapter 6. Configuring the Squid caching proxy server
To reduce bandwidth and quickly load web pages, use Squid. It is a caching proxy server that can act as a proxy for HTTP, HTTPS, and FTP protocols, and allows access authentication and restrictions. For details, see the configuration parameters at /usr/share/doc/squid/squid.conf.documented.
6.1. Setting up Squid as a caching proxy without authentication Copy linkLink copied to clipboard!
To simplify access for users while improving bandwidth efficiency and response time by using the content caching, configure Squid as a caching proxy without authentication. You need to limit access to the proxy, based on IP ranges only.
Prerequisites
-
The
squidpackage includes the/etc/squid/squid.conffile. If you edited this file before, remove and reinstall the package.
Procedure
Install the
squidpackage:# dnf install squidEdit the
/etc/squid/squid.conffile:# vi /etc/squid/squid.confAdapt the
localnetaccess control lists (ACL) to match the allowed IP ranges that can access the proxy:acl localnet src 192.0.2.0/24 acl localnet 2001:db8:1::/64By default, the
/etc/squid/squid.conffile includes thehttp_access allow localnetrule. This rule uses the proxy from all IP ranges specified inlocalnetACLs. You must specify alllocalnetACLs before thehttp_access allow localnetrule.ImportantRemove all existing
acl localnetentries that do not match your environment.To grant users to use the HTTPS protocol on other ports, add an ACL for each of these ports:
acl SSL_ports port port_numberThe following ACL exists in the default configuration and defines
443as a port that uses the HTTPS protocol:acl SSL_ports port 443Update the list of
acl Safe_portsrules to configure to which ports Squid can establish a connection:acl Safe_ports port 21 acl Safe_ports port 80 acl Safe_ports port 443For example, to configure that clients can only access resources on ports
21(FTP),80(HTTP), and443(HTTPS) over the proxy, keep only the followingacl Safe_portsstatements in the configuration file. By default, the configuration file includes thehttp_access deny !Safe_portsrule that defines access denial to ports that are not defined inSafe_portsACLs.Configure the cache type, the path to the cache directory, the cache size, and further cache type-specific settings in the
cache_dirparameter:cache_dir ufs /var/spool/squid 10000 16 256With these settings:
-
Squid uses the
ufscache type. -
Squid stores its cache in the
/var/spool/squid/directory. -
The cache grows up to
10000MB. -
Squid creates
16level-1 sub-directories in the/var/spool/squid/directory. Squid creates
256sub-directories in each level-1 directory.If you do not set a
cache_dirdirective, Squid stores the cache in memory.
-
Squid uses the
If you set a different cache directory than
/var/spool/squid/in thecache_dirparameter:Create the cache directory:
# mkdir -p <path_to_cache_directory>Configure the permissions for the cache directory:
# chown squid:squid <path_to_cache_directory>If the
semanageutility is not available, install thepolicycoreutils-python-utilspackage:# dnf install policycoreutils-python-utilsSet the
squid_cache_tcontext for the cache directory if SELinux is in theenforcingmode:# semanage fcontext -a -t squid_cache_t "<path_to_cache_directory>(/.)?"* # restorecon -Rv <path_to_cache_directory>
Open the
3128port in the firewall:# firewall-cmd --permanent --add-port=3128/tcp # firewall-cmd --reloadEnable and start the
squidservice:# systemctl enable --now squid
Verification
Download a web page by using the
curlutility to verify that the proxy works correctly:# curl -O -L "https://www.redhat.com/index.html" -x "proxy.example.com:3128"If
curldoes not display any error and theindex.htmlfile gets downloaded to the current directory, the proxy works.
6.2. Setting up Squid as a caching proxy with LDAP authentication Copy linkLink copied to clipboard!
To allow only authenticated users to use the proxy, configure Squid as a caching proxy with the Lightweight Directory Access Protocol (LDAP) authentication.
Prerequisites
-
You have installed the
squidpackage. -
The
squidpackage includes the/etc/squid/squid.conffile. If you edited this file before, remove and reinstall the package. -
A service user, such as
uid=proxy_user,cn=users,cn=accounts,dc=example,dc=comexists in the LDAP directory. Squid uses this account only to search for the authenticating user. If the authenticating user exists, Squid binds this user to the directory to verify the authentication.
Procedure
Edit the
/etc/squid/squid.conffile:To configure the
basic_ldap_authhelper utility, add the following configuration entry to the top of/etc/squid/squid.conf:auth_param basic program /usr/lib64/squid/basic_ldap_auth -b "cn=users,cn=accounts,dc=example,dc=com" -D "uid=proxy_user,cn=users,cn=accounts,dc=example,dc=com" -W /etc/squid/ldap_password -f "(&(objectClass=person)(uid=%s))" -ZZ -H ldap://ldap_server.example.com:389-
-b base_DNsets the LDAP search base. -
-D proxy_service_user_DNsets the distinguished name (DN) of the account Squid uses to search for the authenticating user in the directory. -
-W path_to_password_filesets the path to the file that has the password of the proxy service user. Using a password file prevents that the password is visible in the operating system’s process list. -f LDAP_filterspecifies the LDAP search filter. Squid replaces the%svariable with the user name provided by the authenticating user.The
(&(objectClass=person)(uid=%s))filter in the example defines that the user name must match the value set in theuidattribute and that the directory entry includes thepersonobject class.-ZZenforces a TLS-encrypted connection over the LDAP protocol using theSTARTTLScommand. Omit the-ZZin the following situations:- The LDAP server does not support encrypted connections.
- The port specified in the URL uses the Lightweight Directory Access Protocol Secure (LDAPS) protocol.
- The -H LDAP_URL parameter specifies the protocol, the hostname or IP address, and the port of the LDAP server in URL format.
-
Add the following Access Control List (ACL) and rule to configure that Squid allows only authenticated users to use the proxy:
acl ldap-auth proxy_auth REQUIRED http_access allow ldap-authImportantSpecify these settings before the
http_access denyall rule.Remove the following rule to disable bypassing the proxy authentication from IP ranges specified in
localnetACLs:http_access allow localnetAdd an ACL for each of these ports so that users can use the HTTPS protocol on other ports:
acl SSL_ports port port_numberFor example, the following ACL exists in the default configuration and defines
443as a port that uses the HTTPS protocol:acl SSL_ports port 443Update the list of
acl Safe_portsrules to configure to which ports Squid can establish a connection:acl Safe_ports port 21 acl Safe_ports port 80 acl Safe_ports port 443By default, the configuration has the
http_access deny !Safe_portsrule that defines access denial to ports that are not defined inSafe_ports ACLs. For example, to configure that clients allowed to use the proxy can only access resources on port 21 (FTP), 80 (HTTP), and 443 (HTTPS), keep only the followingacl Safe_portsstatements in the configuration.Configure the cache type, the path to the cache directory, the cache size, and further cache type-specific settings in the
cache_dirparameter:cache_dir ufs /var/spool/squid 10000 16 256With these settings:
-
Squid uses the
ufscache type. -
Squid stores its cache in the
/var/spool/squid/directory. -
The cache grows up to
10000MB. -
Squid creates
16level-1 sub-directories in the/var/spool/squid/directory. Squid creates
256sub-directories in each level-1 directory.If you do not set a
cache_dirdirective, Squid stores the cache in memory.
-
Squid uses the
If you set a different cache directory than
/var/spool/squid/in thecache_dirparameter:Create the cache directory:
# mkdir -p path_to_cache_directoryConfigure the permissions for the cache directory:
# chown squid:squid path_to_cache_directoryIf you run SELinux in
enforcingmode, set thesquid_cache_tcontext for the cache directory:# semanage fcontext -a -t squid_cache_t "path_to_cache_directory(/.*)?" # restorecon -Rv path_to_cache_directoryIf the
semanageutility is not available on your system, install thepolicycoreutils-python-utilspackage.
Store the password of the LDAP service user in the
/etc/squid/ldap_passwordfile, and set appropriate permissions for the file:# echo "password" > /etc/squid/ldap_password # chown root:squid /etc/squid/ldap_password # chmod 640 /etc/squid/ldap_passwordOpen the
3128port in the firewall:# firewall-cmd --permanent --add-port=3128/tcp # firewall-cmd --reloadEnable and start the
squidservice:# systemctl enable --now squid
Verification
To verify that the proxy works correctly, download a web page:
# curl -O -L "https://www.redhat.com/index.html" -x "user_name:password@proxy.example.com:3128"If curl does not display any error and the
index.htmlfile was downloaded to the current directory, the proxy works.
Troubleshooting
To verify that the helper utility works correctly:
Manually start the helper utility with the same settings you used in the
auth_paramparameter:# /usr/lib64/squid/basic_ldap_auth -b "cn=users,cn=accounts,dc=example,dc=com" -D "uid=proxy_user,cn=users,cn=accounts,dc=example,dc=com" -W /etc/squid/ldap_password -f "(&(objectClass=person)(uid=%s))" -ZZ -H ldap://ldap_server.example.com:389Enter a valid user name and password, and press
Enter:user_name passwordIf the helper utility returns
OK, authentication succeeded.
6.3. Setting up Squid as a caching proxy with Kerberos authentication Copy linkLink copied to clipboard!
To authenticate users to an Active Directory (AD) by using Kerberos, configure Squid as a caching proxy. Only authenticated users can use the proxy.
Prerequisites
-
The
squidpackage includes the/etc/squid/squid.conffile. If you edited this file before, remove and reinstall the package. - The server on which you want to install Squid is a member of the AD domain.
Procedure
Install the packages:
# dnf install squid krb5-workstationAuthenticate as the AD domain administrator:
# kinit administrator@AD.EXAMPLE.COMCreate a keytab for Squid, store it in the
/etc/squid/HTTP.keytabfile, and add theHTTPservice principal to the keytab:# export KRB5_KTNAME=FILE:/etc/squid/HTTP.keytab # net ads keytab CREATE -U administrator # net ads keytab ADD HTTP -U administratorOptional: If system is initially joined to the AD domain with realm (through
adcli), addHTTPprincipal and create a keytab file for Squid:Add the
HTTPservice principal to the default keytab file/etc/krb5.keytaband verify:# adcli update -vvv --domain=ad.example.com --computer-name=PROXY --add-service-principal="HTTP/proxy.ad.example.com" -C # klist -kte /etc/krb5.keytab | grep -i HTTPLoad the
/etc/krb5.keytabfile, remove all service principals exceptHTTP, and save the remaining principals into the/etc/squid/HTTP.keytabfile:# ktutil ktutil: rkt /etc/krb5.keytab ktutil: l -e slot | KVNO | Principal ----------------------------------------------------------------------------- 1 | 2 | PROXY$@AD.EXAMPLE.COM (aes128-cts-hmac-sha1-96) 2 | 2 | PROXY$@AD.EXAMPLE.COM (aes256-cts-hmac-sha1-96) 3 | 2 | host/PROXY@AD.EXAMPLE.COM (aes128-cts-hmac-sha1-96) 4 | 2 | host/PROXY@AD.EXAMPLE.COM (aes256-cts-hmac-sha1-96) 5 | 2 | host/proxy.ad.example.com@AD.EXAMPLE.COM (aes128-cts-hmac-sha1-96) 6 | 2 | host/proxy.ad.example.com@AD.EXAMPLE.COM (aes256-cts-hmac-sha1-96) 7 | 2 | HTTP/proxy.ad.example.com@AD.EXAMPLE.COM (aes128-cts-hmac-sha1-96) 8 | 2 | HTTP/proxy.ad.example.com@AD.EXAMPLE.COM (aes256-cts-hmac-sha1-96)In the interactive terminal of
ktutil, you can use the different options, until you remove all unwanted principals from the keytab, for example:ktutil: delent 1ktutil: l -e slot | KVNO | Principal ------------------------------------------------------------------------------- 1 | 2 | HTTP/proxy.ad.example.com@AD.EXAMPLE.COM (aes128-cts-hmac-sha1-96) 2 | 2 | HTTP/proxy.ad.example.com@AD.EXAMPLE.COM (aes256-cts-hmac-sha1-96) ktutil: wkt /etc/squid/HTTP.keytab ktutil: qWarningThe keys in
/etc/krb5.keytabmight get updated if System Security Services Daemon (SSSD) or Samba/winbind update the machine account password. After the update, the key in/etc/squid/HTTP.keytabcan stop working, and you need to perform thektutilsteps again to copy the new keys into the keytab.
Set the owner of the keytab file to the
squiduser:# chown squid /etc/squid/HTTP.keytabOptional: Verify that the keytab file has the
HTTPservice principal for the fully-qualified domain name (FQDN) of the proxy server:# klist -k /etc/squid/HTTP.keytab Keytab name: FILE:/etc/squid/HTTP.keytab KVNO Principal ---- ------------------- ... 2 HTTP/proxy.ad.example.com@AD.EXAMPLE.COM ...Edit the
/etc/squid/squid.conffile:To configure the
negotiate_kerberos_authhelper utility, add the following configuration entry to the top of/etc/squid/squid.conf:auth_param negotiate program /usr/lib64/squid/negotiate_kerberos_auth -k /etc/squid/HTTP.keytab -s HTTP/proxy.ad.example.com@AD.EXAMPLE.COMThe following describes the parameters passed to the
negotiate_kerberos_authhelper utility:-
-k filesets the path to the key tab file. Note that the squid user must have read permissions on this file. -s HTTP/host_name@kerberos_realmsets the Kerberos principal that Squid uses.Optionally, you can enable logging by passing one or both of the following parameters to the helper utility:
-
-ilogs informational messages, such as the authenticating user. -denables debug logging.Squid logs the debugging information from the helper utility to the
/var/log/squid/cache.logfile.
-
Add the following Access Control List (ACL) and rule to configure that Squid allows only authenticated users to use the proxy:
acl kerb-auth proxy_auth REQUIRED http_access allow kerb-authImportantSpecify these settings before the
http_access deny allrule.Remove the following rule to disable bypassing the proxy authentication from IP ranges specified in
localnetACLs:http_access allow localnetIf users should be able to use the HTTPS protocol also on other ports, add an ACL for each of these port:
acl SSL_ports port port_numberFor example, the following ACL exists in the default configuration and defines
443as a port that uses the HTTPS protocol:acl SSL_ports port 443Update the list of
acl Safe_portsrules to configure to which ports Squid can establish a connection. For example, to configure that clients using the proxy can only access resources on port 21 (FTP), 80 (HTTP), and 443 (HTTPS), keep only the followingacl Safe_portsstatements in the configuration:acl Safe_ports port 21 acl Safe_ports port 80 acl Safe_ports port 443By default, the configuration has the
http_access deny !Safe_portsrule that defines access denial to ports that are not defined inSafe_portsACLs.Configure the cache type, the path to the cache directory, the cache size, and further cache type-specific settings in the
cache_dirparameter:cache_dir ufs /var/spool/squid 10000 16 256With these settings:
-
Squid uses the
ufscache type. -
Squid stores its cache in the
/var/spool/squid/directory. -
The cache grows up to
10000MB. -
Squid creates
16level-1 sub-directories in the/var/spool/squid/directory. Squid creates
256sub-directories in each level-1 directory.If you do not set a
cache_dirdirective, Squid stores the cache in memory.
-
Squid uses the
If you set a different cache directory than
/var/spool/squid/in thecache_dirparameter:Create the cache directory:
# mkdir -p path_to_cache_directoryConfigure the permissions for the cache directory:
# chown squid:squid path_to_cache_directoryIf you run SELinux in
enforcingmode, set thesquid_cache_tcontext for the cache directory:# semanage fcontext -a -t squid_cache_t "path_to_cache_directory(/.*)?" # restorecon -Rv path_to_cache_directoryIf the
semanageutility is not available on your system, install thepolicycoreutils-python-utilspackage.
Open the
3128port in the firewall:# firewall-cmd --permanent --add-port=3128/tcp # firewall-cmd --reloadEnable and start the
squidservice:# systemctl enable --now squid
Verification
To verify that the proxy works correctly, download a web page using the
curlutility:# curl -O -L "https://www.redhat.com/index.html" --proxy-negotiate -u : -x "proxy.ad.example.com:3128"If
curldoes not display any error and theindex.htmlfile exists in the current directory, the proxy works.
Troubleshooting steps
Obtain a Kerberos ticket for the AD account:
# kinit user@AD.EXAMPLE.COMOptional: Display the ticket:
# klistUse the
negotiate_kerberos_auth_testutility to test the authentication:# /usr/lib64/squid/negotiate_kerberos_auth_test proxy.ad.example.comIf the helper utility returns a token, the authentication succeeded:
Token: YIIFtAYGKwYBBQUCoIIFqDC...
6.4. Configuring a domain deny list in Squid Copy linkLink copied to clipboard!
To block access to specific domains, configure a domain deny list in Squid. It is useful to block domains that are either malicious or spam.
Prerequisites
- You have configured Squid as a caching proxy, and users can use the proxy.
Procedure
Edit following settings in the
/etc/squid/squid.conffile:acl domain_deny_list dstdomain "/etc/squid/domain_deny_list.txt" http_access deny all domain_deny_listImportantAdd these entries before the first
http_access allowstatement that allows access to users or clients.Create the
/etc/squid/domain_deny_list.txtfile and add the domains you want to block. For example, to block access toexample.comincluding subdomains and to blockexample.netonly, add:.example.com example.netImportantIf you referred to the
/etc/squid/domain_deny_list.txtfile in the squid configuration, this file must not be empty. If the file is empty, Squid fails to start.Restart the
squidservice:# systemctl restart squid
6.5. Configuring the Squid service to listen on a specific port or IP address Copy linkLink copied to clipboard!
To configure the Squid service to listen on a specific port or IP address, edit the /etc/squid/squid.conf file. By default, the Squid proxy service listens on the 3128 port on all network interfaces.
Prerequisites
-
You have installed the
squidpackage.
Procedure
Edit the
/etc/squid/squid.conffile:To set the port on which the Squid service listens, set the port number in the
http_portparameter. For example, to set the port to8080, enter:http_port 8080To configure on which IP address the Squid service listens, set the IP address and port number in the
http_portparameter. For example, to configure that Squid listens only on the192.0.2.1IP address on port3128, enter:http_port 192.0.2.1:3128Add multiple
http_portparameters to the configuration file to configure that Squid listens on multiple ports and IP addresses:http_port 192.0.2.1:3128http_port 192.0.2.1:8080
If you configured that Squid uses a different port than the default
3128:Open the port in the firewall:
# firewall-cmd --permanent --add-port=port_number/tcp# firewall-cmd --reloadInstall the
policycoreutils-python-utilspackage to use thesemanageutility:# dnf install policycoreutils-python-utilsIf you run SELinux in enforcing mode, assign the port to the
squid_port_tport type definition:# semanage port -a -t squid_port_t -p tcp <port_number>
Restart the
squidservice:# systemctl restart squid