Deploying web servers and reverse proxies
Setting up and configuring web servers and reverse proxies in Red Hat Enterprise Linux 9
Abstract
Providing feedback on Red Hat documentation Copy linkLink copied to clipboard!
We appreciate your feedback on our documentation. Let us know how we can improve it.
Submitting feedback through Jira (account required)
- Log in to the Jira website.
- Click Create in the top navigation bar
- Enter a descriptive title in the Summary field.
- Enter your suggestion for improvement in the Description field. Include links to the relevant parts of the documentation.
- Click Create at the bottom of the dialogue.
Chapter 1. Setting up the Apache HTTP web server Copy linkLink copied to clipboard!
1.1. Introduction to the Apache HTTP web server Copy linkLink copied to clipboard!
A web server is a network service that serves content to a client over the web. This typically means web pages, but any other documents can be served as well. Web servers are also known as HTTP servers, as they use the hypertext transport protocol (HTTP).
The Apache HTTP Server, httpd, is an open source web server developed by the Apache Software Foundation.
If you are upgrading from a previous release of Red Hat Enterprise Linux, you have to update the httpd service configuration accordingly. This section reviews some of the newly added features, and guides you through the update of prior configuration files.
1.2. Notable changes in the Apache HTTP Server Copy linkLink copied to clipboard!
RHEL 9 provides version 2.4.62 of the Apache HTTP Server. Notable changes over version 2.4.37 distributed with RHEL 8 include:
Apache HTTP Server Control Interface (
apachectl):-
The
systemctlpager is now disabled forapachectl statusoutput. -
The
apachectlcommand now fails instead of giving a warning if you pass additional arguments. -
The
apachectl graceful-stopcommand now returns immediately. -
The
apachectl configtestcommand now executes thehttpd -tcommand without changing the SELinux context. -
The
apachectl(8)man page in RHEL now fully documents differences from upstreamapachectl.
-
The
Apache eXtenSion tool (
apxs):-
The
/usr/bin/apxscommand no longer uses or exposes compiler optimisation flags as applied when building thehttpdpackage. You can now use the/usr/lib64/httpd/build/vendor-apxscommand to apply the same compiler flags as used to buildhttpd. To use thevendor-apxscommand, you must install theredhat-rpm-configpackage first.
-
The
Apache modules:
-
The
mod_luamodule is now provided in a separate package. -
The
mod_phpmodule provided with PHP for use with the Apache HTTP Server has been removed. Since RHEL 8, PHP scripts are run using the FastCGI Process Manager (php-fpm) by default. For more information, see Using PHP with the Apache HTTP Server.
-
The
Configuration syntax changes:
-
In the deprecated
Allowdirective provided by themod_access_compatmodule, a comment (the#character) now triggers a syntax error instead of being silently ignored.
-
In the deprecated
Other changes:
- Kernel thread IDs are now used directly in error log messages, making them both accurate and more concise.
- Many minor enhancements and bug fixes.
- Several new interfaces are available to module authors.
There are no backwards-incompatible changes to the httpd module API since RHEL 8.
Apache HTTP Server 2.4 is the initial version of this Application Stream, which you can install easily as an RPM package.
1.3. The Apache configuration files Copy linkLink copied to clipboard!
The httpd, by default, reads the configuration files after start. You can see the list of the locations of configuration files in the table below.
| Path | Description |
|---|---|
|
| The main configuration file. |
|
| An auxiliary directory for configuration files that are included in the main configuration file. |
|
| An auxiliary directory for configuration files which load installed dynamic modules packaged in Red Hat Enterprise Linux. In the default configuration, these configuration files are processed first. |
Although the default configuration is suitable for most situations, you can use also other configuration options. For any changes to take effect, restart the web server first.
To check the configuration for possible errors, type the following at a shell prompt:
apachectl configtest
# apachectl configtest
Syntax OK
To make the recovery from mistakes easier, make a copy of the original file before editing it.
1.4. Managing the httpd service Copy linkLink copied to clipboard!
This section describes how to start, stop, and restart the httpd service.
Prerequisites
- The Apache HTTP Server is installed.
Procedure
To start the
httpdservice, enter:systemctl start httpd
# systemctl start httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow To stop the
httpdservice, enter:systemctl stop httpd
# systemctl stop httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow To restart the
httpdservice, enter:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
1.5. Setting up a single-instance Apache HTTP Server Copy linkLink copied to clipboard!
You can set up a single-instance Apache HTTP Server to serve static HTML content.
Follow the procedure if the web server should provide the same content for all domains associated with the server. If you want to provide different content for different domains, set up name-based virtual hosts. For details, see Configuring Apache name-based virtual hosts.
Procedure
Install the
httpdpackage:dnf install httpd
# dnf install httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you use
firewalld, open the TCP port80in the local firewall:firewall-cmd --permanent --add-port=80/tcp firewall-cmd --reload
# firewall-cmd --permanent --add-port=80/tcp # firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enable and start the
httpdservice:systemctl enable --now httpd
# systemctl enable --now httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Add HTML files to the
/var/www/html/directory.NoteWhen adding content to
/var/www/html/, files and directories must be readable by the user under whichhttpdruns by default. The content owner can be either therootuser androotuser group, or another user or group of the administrator’s choice. If the content owner is therootuser androotuser group, the files must be readable by other users. The SELinux context for all the files and directories must behttpd_sys_content_t, which is applied by default to all content within the/var/wwwdirectory.
Verification
Connect with a web browser to
http://server_IP_or_host_name/.If the
/var/www/html/directory is empty or does not contain anindex.htmlorindex.htmfile, Apache displays theRed Hat Enterprise Linux Test Page. If/var/www/html/contains HTML files with a different name, you can load them by entering the URL to that file, such ashttp://server_IP_or_host_name/example.html.
1.6. Configuring Apache name-based virtual hosts Copy linkLink copied to clipboard!
Name-based virtual hosts enable Apache to serve different content for different domains that resolve to the IP address of the server.
You can set up a virtual host for both the example.com and example.net domain with separate document root directories. Both virtual hosts serve static HTML content.
Prerequisites
Clients and the web server resolve the
example.comandexample.netdomain to the IP address of the web server.Note that you must manually add these entries to your DNS server.
Procedure
Install the
httpdpackage:dnf install httpd
# dnf install httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the
/etc/httpd/conf/httpd.conffile:Append the following virtual host configuration for the
example.comdomain:Copy to Clipboard Copied! Toggle word wrap Toggle overflow These settings configure the following:
-
All settings in the
<VirtualHost *:80>directive are specific for this virtual host. -
DocumentRootsets the path to the web content of the virtual host. ServerNamesets the domains for which this virtual host serves content.To set multiple domains, add the
ServerAliasparameter to the configuration and specify the additional domains separated with a space in this parameter.-
CustomLogsets the path to the access log of the virtual host. ErrorLogsets the path to the error log of the virtual host.NoteApache uses the first virtual host found in the configuration also for requests that do not match any domain set in the
ServerNameandServerAliasparameters. This also includes requests sent to the IP address of the server.
-
All settings in the
Append a similar virtual host configuration for the
example.netdomain:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the document roots for both virtual hosts:
mkdir /var/www/example.com/ mkdir /var/www/example.net/
# mkdir /var/www/example.com/ # mkdir /var/www/example.net/Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you set paths in the
DocumentRootparameters that are not within/var/www/, set thehttpd_sys_content_tcontext on both document roots:semanage fcontext -a -t httpd_sys_content_t "/srv/example.com(/.*)?" restorecon -Rv /srv/example.com/ semanage fcontext -a -t httpd_sys_content_t "/srv/example.net(/.\*)?" restorecon -Rv /srv/example.net/
# semanage fcontext -a -t httpd_sys_content_t "/srv/example.com(/.*)?" # restorecon -Rv /srv/example.com/ # semanage fcontext -a -t httpd_sys_content_t "/srv/example.net(/.\*)?" # restorecon -Rv /srv/example.net/Copy to Clipboard Copied! Toggle word wrap Toggle overflow These commands set the
httpd_sys_content_tcontext on the/srv/example.com/and/srv/example.net/directory.Note that you must install the
policycoreutils-python-utilspackage to run therestoreconcommand.If you use
firewalld, open port80in the local firewall:firewall-cmd --permanent --add-port=80/tcp firewall-cmd --reload
# firewall-cmd --permanent --add-port=80/tcp # firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enable and start the
httpdservice:systemctl enable --now httpd
# systemctl enable --now httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Create a different example file in each virtual host’s document root:
echo "vHost example.com" > /var/www/example.com/index.html echo "vHost example.net" > /var/www/example.net/index.html
# echo "vHost example.com" > /var/www/example.com/index.html # echo "vHost example.net" > /var/www/example.net/index.htmlCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Use a browser and connect to
http://example.com. The web server shows the example file from theexample.comvirtual host. -
Use a browser and connect to
http://example.net. The web server shows the example file from theexample.netvirtual host.
1.7. Configuring Kerberos authentication for the Apache HTTP web server Copy linkLink copied to clipboard!
To perform Kerberos authentication in the Apache HTTP web server, RHEL 9 uses the mod_auth_gssapi Apache module. The Generic Security Services API (GSSAPI) is an interface for applications that make requests to use security libraries, such as Kerberos. The gssproxy service allows to implement privilege separation for the httpd server, which optimizes this process from the security point of view.
The mod_auth_gssapi module replaces the removed mod_auth_kerb module.
Prerequisites
-
The
httpd,mod_auth_gssapiandgssproxypackages are installed. -
The Apache web server is set up and the
httpdservice is running.
1.7.1. Setting up GSS-Proxy in an IdM environment Copy linkLink copied to clipboard!
This procedure describes how to set up GSS-Proxy to perform Kerberos authentication in the Apache HTTP web server.
Procedure
Enable access to the
keytabfile of HTTP/<SERVER_NAME>@realm principal by creating the service principal:ipa service-add HTTP/<SERVER_NAME>
# ipa service-add HTTP/<SERVER_NAME>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Retrieve the
keytabfor the principal stored in the/etc/gssproxy/http.keytabfile:ipa-getkeytab -s $(awk '/^server =/ {print $3}' /etc/ipa/default.conf) -k /etc/gssproxy/http.keytab -p HTTP/$(hostname -f)# ipa-getkeytab -s $(awk '/^server =/ {print $3}' /etc/ipa/default.conf) -k /etc/gssproxy/http.keytab -p HTTP/$(hostname -f)Copy to Clipboard Copied! Toggle word wrap Toggle overflow This step sets permissions to 400, thus only the
rootuser has access to thekeytabfile. Theapacheuser does not.Create the
/etc/gssproxy/80-httpd.conffile with the following content:[service/HTTP] mechs = krb5 cred_store = keytab:/etc/gssproxy/http.keytab cred_store = ccache:/var/lib/gssproxy/clients/krb5cc_%U euid = apache
[service/HTTP] mechs = krb5 cred_store = keytab:/etc/gssproxy/http.keytab cred_store = ccache:/var/lib/gssproxy/clients/krb5cc_%U euid = apacheCopy to Clipboard Copied! Toggle word wrap Toggle overflow Restart and enable the
gssproxyservice:systemctl restart gssproxy.service systemctl enable gssproxy.service
# systemctl restart gssproxy.service # systemctl enable gssproxy.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
1.8. Configuring TLS encryption on an Apache HTTP Server Copy linkLink copied to clipboard!
By default, Apache provides content to clients using an unencrypted HTTP connection. This section describes how to enable TLS encryption and configure frequently used encryption-related settings on an Apache HTTP Server.
Prerequisites
- The Apache HTTP Server is installed and running.
1.8.1. Adding TLS encryption to an Apache HTTP Server Copy linkLink copied to clipboard!
You can enable TLS encryption on an Apache HTTP Server for the example.com domain.
Prerequisites
- The Apache HTTP Server is installed and running.
The private key is stored in the
/etc/pki/tls/private/example.com.keyfile.For details about creating a private key and certificate signing request (CSR), as well as how to request a certificate from a certificate authority (CA), see your CA’s documentation. Alternatively, if your CA supports the ACME protocol, you can use the
mod_mdmodule to automate retrieving and provisioning TLS certificates.-
The TLS certificate is stored in the
/etc/pki/tls/certs/example.com.crtfile. If you use a different path, adapt the corresponding steps of the procedure. -
The CA certificate is stored in the
/etc/pki/tls/certs/ca.crtfile. If you use a different path, adapt the corresponding steps of the procedure. - Clients and the web server resolve the host name of the server to the IP address of the web server.
- If the server runs RHEL 9.2 or later and the FIPS mode is enabled, clients must either support the Extended Master Secret (EMS) extension or use TLS 1.3. TLS 1.2 connections without EMS fail. For more information, see the Red Hat Knowledgebase solution TLS extension "Extended Master Secret" enforced.
Procedure
Install the
mod_sslpackage:dnf install mod_ssl
# dnf install mod_sslCopy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the
/etc/httpd/conf.d/ssl.conffile and add the following settings to the<VirtualHost _default_:443>directive:Set the server name:
ServerName example.com
ServerName example.comCopy to Clipboard Copied! Toggle word wrap Toggle overflow
The server name must match the entry set in the Common Name field of the certificate.
Optional: If the certificate contains additional host names in the
Subject Alt Names(SAN) field, you can configuremod_sslto provide TLS encryption also for these host names. To configure this, add theServerAliasesparameter with corresponding names:ServerAlias www.example.com server.example.com
ServerAlias www.example.com server.example.comCopy to Clipboard Copied! Toggle word wrap Toggle overflow Set the paths to the private key, the server certificate, and the CA certificate:
SSLCertificateKeyFile "/etc/pki/tls/private/example.com.key" SSLCertificateFile "/etc/pki/tls/certs/example.com.crt" SSLCACertificateFile "/etc/pki/tls/certs/ca.crt"
SSLCertificateKeyFile "/etc/pki/tls/private/example.com.key" SSLCertificateFile "/etc/pki/tls/certs/example.com.crt" SSLCACertificateFile "/etc/pki/tls/certs/ca.crt"Copy to Clipboard Copied! Toggle word wrap Toggle overflow For security reasons, configure that only the
rootuser can access the private key file:chown root:root /etc/pki/tls/private/example.com.key chmod 600 /etc/pki/tls/private/example.com.key
# chown root:root /etc/pki/tls/private/example.com.key # chmod 600 /etc/pki/tls/private/example.com.keyCopy to Clipboard Copied! Toggle word wrap Toggle overflow WarningIf the private key was accessed by unauthorized users, revoke the certificate, create a new private key, and request a new certificate. Otherwise, the TLS connection is no longer secure.
If you use
firewalld, open port443in the local firewall:firewall-cmd --permanent --add-port=443/tcp firewall-cmd --reload
# firewall-cmd --permanent --add-port=443/tcp # firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
httpdservice:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
NoteIf you protected the private key file with a password, you must enter this password each time when the
httpdservice starts.
Verification
-
Use a browser and connect to
https://example.com.
1.8.2. Setting the supported TLS protocol versions on an Apache HTTP Server Copy linkLink copied to clipboard!
By default, the Apache HTTP Server on RHEL uses the system-wide crypto policy that defines safe default values, which are also compatible with recent browsers. For example, the DEFAULT policy defines that only the TLSv1.2 and TLSv1.3 protocol versions are enabled in apache.
You can manually configure which TLS protocol versions your Apache HTTP Server supports. Follow the procedure if your environment requires to enable only specific TLS protocol versions, for example:
-
If your environment requires that clients can also use the weak
TLS1(TLSv1.0) orTLS1.1protocol. -
If you want to configure that Apache only supports the
TLSv1.2orTLSv1.3protocol.
Prerequisites
- TLS encryption is enabled on the server as described in Adding TLS encryption to an Apache HTTP server.
- If the server runs RHEL 9.2 or later and the FIPS mode is enabled, clients must either support the Extended Master Secret (EMS) extension or use TLS 1.3. TLS 1.2 connections without EMS fail. For more information, see the Red Hat Knowledgebase solution TLS extension "Extended Master Secret" enforced.
Procedure
Edit the
/etc/httpd/conf/httpd.conffile, and add the following setting to the<VirtualHost>directive for which you want to set the TLS protocol version. For example, to enable only theTLSv1.3protocol:SSLProtocol -All TLSv1.3
SSLProtocol -All TLSv1.3Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
httpdservice:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Use the following command to verify that the server supports
TLSv1.3:openssl s_client -connect example.com:443 -tls1_3
# openssl s_client -connect example.com:443 -tls1_3Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the following command to verify that the server does not support
TLSv1.2:openssl s_client -connect example.com:443 -tls1_2
# openssl s_client -connect example.com:443 -tls1_2Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the server does not support the protocol, the command returns an error:
140111600609088:error:1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol version:ssl/record/rec_layer_s3.c:1543:SSL alert number 70
140111600609088:error:1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol version:ssl/record/rec_layer_s3.c:1543:SSL alert number 70Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Optional: Repeat the command for other TLS protocol versions.
1.8.3. Setting the supported ciphers on an Apache HTTP Server Copy linkLink copied to clipboard!
By default, the Apache HTTP Server uses the system-wide crypto policy that defines safe default values, which are also compatible with recent browsers. For the list of ciphers the system-wide crypto allows, see the /etc/crypto-policies/back-ends/openssl.config file.
You can manually configure which ciphers your Apache HTTP Server supports. Follow the procedure if your environment requires specific ciphers.
Prerequisites
- TLS encryption is enabled on the server as described in Adding TLS encryption to an Apache HTTP server.
Procedure
Edit the
/etc/httpd/conf/httpd.conffile, and add theSSLCipherSuiteparameter to the<VirtualHost>directive for which you want to set the TLS ciphers:SSLCipherSuite "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:!SHA1:!SHA256"
SSLCipherSuite "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:!SHA1:!SHA256"Copy to Clipboard Copied! Toggle word wrap Toggle overflow This example enables only the
EECDH+AESGCM,EDH+AESGCM,AES256+EECDH, andAES256+EDHciphers and disables all ciphers which use theSHA1andSHA256message authentication code (MAC).Restart the
httpdservice:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To display the list of ciphers the Apache HTTP Server supports:
Install the
nmappackage:dnf install nmap
# dnf install nmapCopy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
nmaputility to display the supported ciphers:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.9. Configuring TLS client certificate authentication Copy linkLink copied to clipboard!
Client certificate authentication enables administrators to allow only users who authenticate using a certificate to access resources on the web server. You can configure client certificate authentication for the /var/www/html/Example/ directory.
If the Apache HTTP Server uses the TLS 1.3 protocol, certain clients require additional configuration. For example, in Firefox, set the security.tls.enable_post_handshake_auth parameter in the about:config menu to true.
Prerequisites
- TLS encryption is enabled on the server as described in Adding TLS encryption to an Apache HTTP server.
Procedure
Edit the
/etc/httpd/conf/httpd.conffile and add the following settings to the<VirtualHost>directive for which you want to configure client authentication:<Directory "/var/www/html/Example/"> SSLVerifyClient require </Directory>
<Directory "/var/www/html/Example/"> SSLVerifyClient require </Directory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
SSLVerifyClient requiresetting defines that the server must successfully validate the client certificate before the client can access the content in the/var/www/html/Example/directory.Restart the
httpdservice:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Use the
curlutility to access thehttps://example.com/Example/URL without client authentication:curl https://example.com/Example/
$ curl https://example.com/Example/ curl: (56) OpenSSL SSL_read: error:1409445C:SSL routines:ssl3_read_bytes:tlsv13 alert certificate required, errno 0Copy to Clipboard Copied! Toggle word wrap Toggle overflow The error indicates that the web server requires a client certificate authentication.
Pass the client private key and certificate, as well as the CA certificate to
curlto access the same URL with client authentication:curl --cacert ca.crt --key client.key --cert client.crt https://example.com/Example/
$ curl --cacert ca.crt --key client.key --cert client.crt https://example.com/Example/Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the request succeeds,
curldisplays theindex.htmlfile stored in the/var/www/html/Example/directory.
1.10. Securing web applications on a web server using ModSecurity Copy linkLink copied to clipboard!
ModSecurity is an open source web application firewall (WAF) supported by various web servers such as Apache, Nginx, and IIS, which reduces security risks in web applications. ModSecurity provides customizable rule sets for configuring your server.
The mod_security-crs package contains the core rule set (CRS) with rules against cross-website scripting, bad user agents, SQL injection, Trojans, session hijacking, and other exploits.
1.10.1. Deploying the ModSecurity web-based application firewall for Apache Copy linkLink copied to clipboard!
To reduce risks related to running web-based applications on your web server by deploying ModSecurity, install the mod_security and mod_security_crs packages for the Apache HTTP server. The mod_security_crs package provides the core rule set (CRS) for the ModSecurity web-based application firewall (WAF) module.
Procedure
Install the
mod_security,mod_security_crs, andhttpdpackages:dnf install -y mod_security mod_security_crs httpd
# dnf install -y mod_security mod_security_crs httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
httpdserver:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify that the ModSecurity web-based application firewall is enabled on your Apache HTTP server:
httpd -M | grep security
# httpd -M | grep security security2_module (shared)Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check that the
/etc/httpd/modsecurity.d/activated_rules/directory contains rules provided bymod_security_crs:ls /etc/httpd/modsecurity.d/activated_rules/
# ls /etc/httpd/modsecurity.d/activated_rules/ ... REQUEST-921-PROTOCOL-ATTACK.conf REQUEST-930-APPLICATION-ATTACK-LFI.conf ...Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.10.2. Adding a custom rule to ModSecurity Copy linkLink copied to clipboard!
If the rules contained in the ModSecurity core rule set (CRS) do not fit your scenario and if you want to prevent additional possible attacks, you can add your custom rules to the rule set used by the ModSecurity web-based application firewall. The following example demonstrates the addition of a simple rule. For creating more complex rules, see the reference manual on the ModSecurity Wiki website.
Prerequisites
- ModSecurity for Apache is installed and enabled.
Procedure
Open the
/etc/httpd/conf.d/mod_security.conffile in a text editor of your choice, for example:vi /etc/httpd/conf.d/mod_security.conf
# vi /etc/httpd/conf.d/mod_security.confCopy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following example rule after the line starting with
SecRuleEngine On:SecRule ARGS:data "@contains evil" "deny,status:403,msg:'param data contains evil data',id:1"
SecRule ARGS:data "@contains evil" "deny,status:403,msg:'param data contains evil data',id:1"Copy to Clipboard Copied! Toggle word wrap Toggle overflow The previous rule forbids the use of resources to the user if the
dataparameter contains theevilstring.- Save the changes, and quit the editor.
Restart the
httpdserver:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Create a
test.htmlpage:echo "mod_security test" > /var/www/html/test.html
# echo "mod_security test" > /var/www/html/test.htmlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
httpdserver:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Request
test.htmlwithout malicious data in theGETvariable of the HTTP request:curl http://localhost/test.html?data=good
$ curl http://localhost/test.html?data=good mod_security testCopy to Clipboard Copied! Toggle word wrap Toggle overflow Request
test.htmlwith malicious data in theGETvariable of the HTTP request:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check the
/var/log/httpd/error_logfile, and locate the log entry about denying access with theparam data containing an evil datamessage:[Wed May 25 08:01:31.036297 2022] [:error] [pid 5839:tid 139874434791168] [client ::1:45658] [client ::1] ModSecurity: Access denied with code 403 (phase 2). String match "evil" at ARGS:data. [file "/etc/httpd/conf.d/mod_security.conf"] [line "4"] [id "1"] [msg "param data contains evil data"] [hostname "localhost"] [uri "/test.html"] [unique_id "Yo4amwIdsBG3yZqSzh2GuwAAAIY"]
[Wed May 25 08:01:31.036297 2022] [:error] [pid 5839:tid 139874434791168] [client ::1:45658] [client ::1] ModSecurity: Access denied with code 403 (phase 2). String match "evil" at ARGS:data. [file "/etc/httpd/conf.d/mod_security.conf"] [line "4"] [id "1"] [msg "param data contains evil data"] [hostname "localhost"] [uri "/test.html"] [unique_id "Yo4amwIdsBG3yZqSzh2GuwAAAIY"]Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.11. Installing the Apache HTTP Server manual Copy linkLink copied to clipboard!
You can install the Apache HTTP Server manual. This manual provides a detailed documentation of, for example:
- Configuration parameters and directives
- Performance tuning
- Authentication settings
- Modules
- Content caching
- Security tips
- Configuring TLS encryption
After installing the manual, you can display it using a web browser.
Prerequisites
- The Apache HTTP Server is installed and running.
Procedure
Install the
httpd-manualpackage:dnf install httpd-manual
# dnf install httpd-manualCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: By default, all clients connecting to the Apache HTTP Server can display the manual. To restrict access to a specific IP range, such as the
192.0.2.0/24subnet, edit the/etc/httpd/conf.d/manual.conffile and add theRequire ip 192.0.2.0/24setting to the<Directory "/usr/share/httpd/manual">directive:<Directory "/usr/share/httpd/manual"> ... Require ip 192.0.2.0/24 ... </Directory><Directory "/usr/share/httpd/manual"> ... Require ip 192.0.2.0/24 ... </Directory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
httpdservice:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
-
To display the Apache HTTP Server manual, connect with a web browser to
http://host_name_or_IP_address/manual/
1.12. Working with Apache modules Copy linkLink copied to clipboard!
The httpd service is a modular application, and you can extend it with a number of Dynamic Shared Objects (DSOs). Dynamic Shared Objects are modules that you can dynamically load or unload at runtime as necessary. You can find these modules in the /usr/lib64/httpd/modules/ directory.
1.12.1. Loading a DSO module Copy linkLink copied to clipboard!
As an administrator, you can choose the functionality to include in the server by configuring which modules the server should load. To load a particular DSO module, use the LoadModule directive. Note that modules provided by a separate package often have their own configuration file in the /etc/httpd/conf.modules.d/ directory.
Prerequisites
-
You have installed the
httpdpackage.
Procedure
Search for the module name in the configuration files in the
/etc/httpd/conf.modules.d/directory:grep mod_ssl.so /etc/httpd/conf.modules.d/*
# grep mod_ssl.so /etc/httpd/conf.modules.d/*Copy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the configuration file in which the module name was found, and uncomment the
LoadModuledirective of the module:LoadModule ssl_module modules/mod_ssl.so
LoadModule ssl_module modules/mod_ssl.soCopy to Clipboard Copied! Toggle word wrap Toggle overflow If the module was not found, for example, because a RHEL package does not provide the module, create a configuration file, such as
/etc/httpd/conf.modules.d/30-example.confwith the following directive:LoadModule ssl_module modules/<custom_module>.so
LoadModule ssl_module modules/<custom_module>.soCopy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
httpdservice:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
1.12.2. Compiling a custom Apache module Copy linkLink copied to clipboard!
You can create your own module and build it with the help of the httpd-devel package, which contains the include files, the header files, and the APache eXtenSion (apxs) utility required to compile a module.
Prerequisites
-
You have the
httpd-develpackage installed.
Procedure
Build a custom module with the following command:
apxs -i -a -c module_name.c
# apxs -i -a -c module_name.cCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
- Load the module the same way as described in Loading a DSO module.
1.13. Exporting a private key and certificates from an NSS database to use them in an Apache web server configuration Copy linkLink copied to clipboard!
Since RHEL 8 we no longer provide the mod_nss module for the Apache web server, and Red Hat recommends using the mod_ssl module. If you store your private key and certificates in a Network Security Services (NSS) database, follow this procedure to extract the key and certificates in Privacy Enhanced Mail (PEM) format.
Chapter 2. Setting up and configuring NGINX Copy linkLink copied to clipboard!
NGINX is a high performance and modular server that you can use, for example, as a:
- Web server
- Reverse proxy
- Load balancer
This section describes how to NGINX in these scenarios.
2.1. Installing and preparing NGINX Copy linkLink copied to clipboard!
In Red Hat Enterprise Linux 9,different versions to NGINX are provided by Application Streams. By using the default configuration, NGINX runs as a web server on port 80 and provides content from the /usr/share/nginx/html/ directory.
Prerequisites
- RHEL 9 is installed.
- The host is subscribed to the Red Hat Customer Portal.
-
The
firewalldservice is enabled and started.
Procedure
Install the
nginxpackage:To install NGINX 1.20 as the initial version of this Application Stream from an RPM package:
dnf install nginx
# dnf install nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf you have previously enabled an NGINX module stream, this command installs the NGINX version from the enabled stream.
To install an alternate later version of NGINX from a module stream:
Display the available NGINX module streams:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enable the selected stream:
dnf module enable nginx:stream_version
# dnf module enable nginx:stream_versionCopy to Clipboard Copied! Toggle word wrap Toggle overflow Install the nginx package:
dnf install nginx
# dnf install nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Open the ports on which NGINX should provide its service in the firewall. For example, to open the default ports for HTTP (port 80) and HTTPS (port 443) in
firewalld, enter:firewall-cmd --permanent --add-port={80/tcp,443/tcp} firewall-cmd --reload# firewall-cmd --permanent --add-port={80/tcp,443/tcp} # firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enable the
nginxservice to start automatically when the system boots:systemctl enable nginx
# systemctl enable nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Start the
nginxservice:systemctl start nginx
# systemctl start nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you do not want to use the default configuration, skip this step, and configure NGINX accordingly before you start the service.
Verification
Use the
dnfutility to verify that thenginxpackage is installed.In case of the NGINX 1.20 RPM package:
dnf list installed nginx
# dnf list installed nginx Installed Packages nginx.x86_64 1:1.20.1-4.el9 @rhel-AppStreamCopy to Clipboard Copied! Toggle word wrap Toggle overflow In case of a selected NGINX module stream:
dnf list installed nginx
# dnf list installed nginx Installed Packages nginx.x86_64 1:1.22.1-3.module+el9.2.0+17617+2f289c6c @rhel-AppStreamCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Ensure that the ports on which NGINX should provide its service are opened in the firewalld:
firewall-cmd --list-ports
# firewall-cmd --list-ports 80/tcp 443/tcpCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that the
nginxservice is enabled:systemctl is-enabled nginx
# systemctl is-enabled nginx enabledCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.2. Configuring NGINX as a web server that provides different content for different domains Copy linkLink copied to clipboard!
By default, NGINX acts as a web server that provides the same content to clients for all domain names associated with the IP addresses of the server. This procedure explains how to configure NGINX:
-
To serve requests to the
example.comdomain with content from the/var/www/example.com/directory -
To serve requests to the
example.netdomain with content from the/var/www/example.net/directory -
To serve all other requests, for example, to the IP address of the server or to other domains associated with the IP address of the server, with content from the
/usr/share/nginx/html/directory
Prerequisites
- NGINX is installed
Clients and the web server resolve the
example.comandexample.netdomain to the IP address of the web server.Note that you must manually add these entries to your DNS server.
Procedure
Edit the
/etc/nginx/nginx.conffile:By default, the
/etc/nginx/nginx.conffile already contains a catch-all configuration. If you have deleted this part from the configuration, re-add the followingserverblock to thehttpblock in the/etc/nginx/nginx.conffile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow These settings configure the following:
-
The
listendirective define which IP address and ports the service listens. In this case, NGINX listens on port80on both all IPv4 and IPv6 addresses. Thedefault_serverparameter indicates that NGINX uses thisserverblock as the default for requests matching the IP addresses and ports. -
The
server_nameparameter defines the host names for which thisserverblock is responsible. Settingserver_nameto_configures NGINX to accept any host name for thisserverblock. -
The
rootdirective sets the path to the web content for thisserverblock.
-
The
Append a similar
serverblock for theexample.comdomain to thehttpblock:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
The
access_logdirective defines a separate access log file for this domain. -
The
error_logdirective defines a separate error log file for this domain.
-
The
Append a similar
serverblock for theexample.netdomain to thehttpblock:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Create the root directories for both domains:
mkdir -p /var/www/example.com/ mkdir -p /var/www/example.net/
# mkdir -p /var/www/example.com/ # mkdir -p /var/www/example.net/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the
httpd_sys_content_tcontext on both root directories:semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?" restorecon -Rv /var/www/example.com/ semanage fcontext -a -t httpd_sys_content_t "/var/www/example.net(/.\*)?" restorecon -Rv /var/www/example.net/
# semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?" # restorecon -Rv /var/www/example.com/ # semanage fcontext -a -t httpd_sys_content_t "/var/www/example.net(/.\*)?" # restorecon -Rv /var/www/example.net/Copy to Clipboard Copied! Toggle word wrap Toggle overflow These commands set the
httpd_sys_content_tcontext on the/var/www/example.com/and/var/www/example.net/directories.Note that you must install the
policycoreutils-python-utilspackage to run therestoreconcommands.Create the log directories for both domains:
mkdir /var/log/nginx/example.com/ mkdir /var/log/nginx/example.net/
# mkdir /var/log/nginx/example.com/ # mkdir /var/log/nginx/example.net/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
nginxservice:systemctl restart nginx
# systemctl restart nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Create a different example file in each virtual host’s document root:
echo "Content for example.com" > /var/www/example.com/index.html echo "Content for example.net" > /var/www/example.net/index.html echo "Catch All content" > /usr/share/nginx/html/index.html
# echo "Content for example.com" > /var/www/example.com/index.html # echo "Content for example.net" > /var/www/example.net/index.html # echo "Catch All content" > /usr/share/nginx/html/index.htmlCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Use a browser and connect to
http://example.com. The web server shows the example content from the/var/www/example.com/index.htmlfile. -
Use a browser and connect to
http://example.net. The web server shows the example content from the/var/www/example.net/index.htmlfile. -
Use a browser and connect to
http://IP_address_of_the_server. The web server shows the example content from the/usr/share/nginx/html/index.htmlfile.
2.3. Adding TLS encryption to an NGINX web server Copy linkLink copied to clipboard!
You can enable TLS encryption on an NGINX web server for the example.com domain.
Prerequisites
- NGINX is installed.
The private key is stored in the
/etc/pki/tls/private/example.com.keyfile.For details about creating a private key and certificate signing request (CSR), as well as how to request a certificate from a certificate authority (CA), see your CA’s documentation.
-
The TLS certificate is stored in the
/etc/pki/tls/certs/example.com.crtfile. If you use a different path, adapt the corresponding steps of the procedure. - The CA certificate has been appended to the TLS certificate file of the server.
- Clients and the web server resolve the host name of the server to the IP address of the web server.
-
Port
443is open in the local firewall. - If the server runs RHEL 9.2 or later and the FIPS mode is enabled, clients must either support the Extended Master Secret (EMS) extension or use TLS 1.3. TLS 1.2 connections without EMS fail. For more information, see the Red Hat Knowledgebase solution TLS extension "Extended Master Secret" enforced.
Procedure
Edit the
/etc/nginx/nginx.conffile, and add the followingserverblock to thehttpblock in the configuration:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Starting with RHEL 9.3, you can use the
ssl_pass_phrase_dialogdirective to configure an external program that is called atnginxstart for each encrypted private key. Add one of the following lines to the/etc/nginx/nginx.conffile:To call an external program for each encrypted private key file, enter:
ssl_pass_phrase_dialog exec:<path_to_program>;
ssl_pass_phrase_dialog exec:<path_to_program>;Copy to Clipboard Copied! Toggle word wrap Toggle overflow NGINX calls this program with the following two arguments:
-
The server name specified in the
server_namesetting. -
One of the following algorithms:
RSA,DSA,EC,DH, orUNKif a cryptographic algorithm cannot be recognized.
-
The server name specified in the
If you want to manually enter a passphrase for each encrypted private key file, enter:
ssl_pass_phrase_dialog builtin;
ssl_pass_phrase_dialog builtin;Copy to Clipboard Copied! Toggle word wrap Toggle overflow This is the default behavior if
ssl_pass_phrase_dialogis not configured.NoteThe
nginxservice fails to start if you use this method but have at least one private key protected by a passphrase. In this case, use one of the other methods.If you want
systemdto prompt for the passphrase for each encrypted private key when you start thenginxservice by using thesystemctlutility, enter:ssl_pass_phrase_dialog exec:/usr/libexec/nginx-ssl-pass-dialog;
ssl_pass_phrase_dialog exec:/usr/libexec/nginx-ssl-pass-dialog;Copy to Clipboard Copied! Toggle word wrap Toggle overflow
For security reasons, configure that only the
rootuser can access the private key file:chown root:root /etc/pki/tls/private/example.com.key chmod 600 /etc/pki/tls/private/example.com.key
# chown root:root /etc/pki/tls/private/example.com.key # chmod 600 /etc/pki/tls/private/example.com.keyCopy to Clipboard Copied! Toggle word wrap Toggle overflow WarningIf the private key was accessed by unauthorized users, revoke the certificate, create a new private key, and request a new certificate. Otherwise, the TLS connection is no longer secure.
Restart the
nginxservice:systemctl restart nginx
# systemctl restart nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
-
Use a browser and connect to
https://example.com
2.4. Configuring NGINX as a reverse proxy for the HTTP traffic Copy linkLink copied to clipboard!
You can configure the NGINX web server to act as a reverse proxy for HTTP traffic. For example, you can use this functionality to forward requests to a specific subdirectory on a remote server. From the client perspective, the client loads the content from the host it accesses. However, NGINX loads the actual content from the remote server and forwards it to the client.
This procedure explains how to forward traffic to the /example directory on the web server to the URL https://example.com.
Prerequisites
- NGINX is installed as described in Installing and preparing NGINX.
- Optional: TLS encryption is enabled on the reverse proxy.
Procedure
Edit the
/etc/nginx/nginx.conffile and add the following settings to theserverblock that should provide the reverse proxy:location /example { proxy_pass https://example.com; }location /example { proxy_pass https://example.com; }Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
locationblock defines that NGINX passes all requests in the/exampledirectory tohttps://example.com.Set the
httpd_can_network_connectSELinux boolean parameter to1to configure that SELinux allows NGINX to forward traffic:setsebool -P httpd_can_network_connect 1
# setsebool -P httpd_can_network_connect 1Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
nginxservice:systemctl restart nginx
# systemctl restart nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
-
Use a browser and connect to
http://host_name/exampleand the content ofhttps://example.comis shown.
2.5. Configuring NGINX as an HTTP load balancer Copy linkLink copied to clipboard!
You can use the NGINX reverse proxy feature to load-balance traffic. This procedure describes how to configure NGINX as an HTTP load balancer that sends requests to different servers, based on which of them has the least number of active connections. If both servers are not available, the procedure also defines a third host for fallback reasons.
Prerequisites
- NGINX is installed as described in Installing and preparing NGINX.
Procedure
Edit the
/etc/nginx/nginx.conffile and add the following settings:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
least_conndirective in the host group namedbackenddefines that NGINX sends requests toserver1.example.comorserver2.example.com, depending on which host has the least number of active connections. NGINX usesserver3.example.comonly as a backup in case that the other two hosts are not available.With the
proxy_passdirective set tohttp://backend, NGINX acts as a reverse proxy and uses thebackendhost group to distribute requests based on the settings of this group.Instead of the
least_connload balancing method, you can specify:- No method to use round robin and distribute requests evenly across servers.
-
ip_hashto send requests from one client address to the same server based on a hash calculated from the first three octets of the IPv4 address or the whole IPv6 address of the client. -
hashto determine the server based on a user-defined key, which can be a string, a variable, or a combination of both. Theconsistentparameter configures that NGINX distributes requests across all servers based on the user-defined hashed key value. -
randomto send requests to a randomly selected server.
Restart the
nginxservice:systemctl restart nginx
# systemctl restart nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 3. Configuring the Squid caching proxy server Copy linkLink copied to clipboard!
Squid is a proxy server that caches content to reduce bandwidth and load web pages more quickly. This chapter describes how to set up Squid as a proxy for the HTTP, HTTPS, and FTP protocol, as well as authentication and restricting access.
3.1. Setting up Squid as a caching proxy without authentication Copy linkLink copied to clipboard!
You can configure Squid as a caching proxy without authentication. The procedure limits access to the proxy based on IP ranges.
Prerequisites
-
The procedure assumes that the
/etc/squid/squid.conffile is as provided by thesquidpackage. If you edited this file before, remove the file and reinstall the package.
Procedure
Install the
squidpackage:dnf install squid
# dnf install squidCopy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the
/etc/squid/squid.conffile:Adapt the
localnetaccess control lists (ACL) to match the IP ranges that should be allowed to use the proxy:acl localnet src 192.0.2.0/24 acl localnet 2001:db8:1::/64
acl localnet src 192.0.2.0/24 acl localnet 2001:db8:1::/64Copy to Clipboard Copied! Toggle word wrap Toggle overflow By default, the
/etc/squid/squid.conffile contains thehttp_access allow localnetrule that allows using the proxy from all IP ranges specified inlocalnetACLs. Note that you must specify alllocalnetACLs before thehttp_access allow localnetrule.ImportantRemove all existing
acl localnetentries that do not match your environment.The following ACL exists in the default configuration and defines
443as a port that uses the HTTPS protocol:acl SSL_ports port 443
acl SSL_ports port 443Copy to Clipboard Copied! Toggle word wrap Toggle overflow If 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_number
acl SSL_ports port port_numberCopy to Clipboard Copied! Toggle word wrap Toggle overflow Update 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 443
acl Safe_ports port 21 acl Safe_ports port 80 acl Safe_ports port 443Copy to Clipboard Copied! Toggle word wrap Toggle overflow By default, the configuration contains 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 256
cache_dir ufs /var/spool/squid 10000 16 256Copy to Clipboard Copied! Toggle word wrap Toggle overflow With 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
# mkdir -p path_to_cache_directoryCopy to Clipboard Copied! Toggle word wrap Toggle overflow Configure the permissions for the cache directory:
chown squid:squid path_to_cache_directory
# chown squid:squid path_to_cache_directoryCopy to Clipboard Copied! Toggle word wrap Toggle overflow If 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_directory
# semanage fcontext -a -t squid_cache_t "path_to_cache_directory(/.*)?" # restorecon -Rv path_to_cache_directoryCopy to Clipboard Copied! Toggle word wrap Toggle overflow If 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 --reload
# firewall-cmd --permanent --add-port=3128/tcp # firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enable and start the
squidservice:systemctl enable --now squid
# systemctl enable --now squidCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To verify that the proxy works correctly, download a web page using the curl utility:
curl -O -L "https://www.redhat.com/index.html" -x "proxy.example.com:3128"
# curl -O -L "https://www.redhat.com/index.html" -x "proxy.example.com:3128"
If curl does not display any error and the index.html file was downloaded to the current directory, the proxy works.
3.2. Setting up Squid as a caching proxy with LDAP authentication Copy linkLink copied to clipboard!
You can configure Squid as a caching proxy that uses LDAP to authenticate users. The procedure configures that only authenticated users can use the proxy.
Prerequisites
-
The procedure assumes that the
/etc/squid/squid.conffile is as provided by thesquidpackage. If you edited this file before, remove the file and reinstall the package. -
An 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 as this user to the directory to verify the authentication.
Procedure
Install the
squidpackage:dnf install squid
# dnf install squidCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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
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:389Copy to Clipboard Copied! Toggle word wrap Toggle overflow The following describes the parameters passed to the
basic_ldap_authhelper utility in the example above:-
-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 contains 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 contains 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 LDAPS protocol.
- The -H LDAP_URL parameter specifies the protocol, the host name or IP address, and the port of the LDAP server in URL format.
-
Add the following 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-auth
acl ldap-auth proxy_auth REQUIRED http_access allow ldap-authCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantSpecify 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 localnet
http_access allow localnetCopy to Clipboard Copied! Toggle word wrap Toggle overflow The following ACL exists in the default configuration and defines
443as a port that uses the HTTPS protocol:acl SSL_ports port 443
acl SSL_ports port 443Copy to Clipboard Copied! Toggle word wrap Toggle overflow If 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_number
acl SSL_ports port port_numberCopy to Clipboard Copied! Toggle word wrap Toggle overflow Update 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 443
acl Safe_ports port 21 acl Safe_ports port 80 acl Safe_ports port 443Copy to Clipboard Copied! Toggle word wrap Toggle overflow By default, the configuration contains the
http_access deny !Safe_portsrule that defines access denial to ports that are not defined inSafe_ports ACLs.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 256
cache_dir ufs /var/spool/squid 10000 16 256Copy to Clipboard Copied! Toggle word wrap Toggle overflow With 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
# mkdir -p path_to_cache_directoryCopy to Clipboard Copied! Toggle word wrap Toggle overflow Configure the permissions for the cache directory:
chown squid:squid path_to_cache_directory
# chown squid:squid path_to_cache_directoryCopy to Clipboard Copied! Toggle word wrap Toggle overflow If 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_directory
# semanage fcontext -a -t squid_cache_t "path_to_cache_directory(/.*)?" # restorecon -Rv path_to_cache_directoryCopy to Clipboard Copied! Toggle word wrap Toggle overflow If 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_password
# echo "password" > /etc/squid/ldap_password # chown root:squid /etc/squid/ldap_password # chmod 640 /etc/squid/ldap_passwordCopy to Clipboard Copied! Toggle word wrap Toggle overflow Open the
3128port in the firewall:firewall-cmd --permanent --add-port=3128/tcp firewall-cmd --reload
# firewall-cmd --permanent --add-port=3128/tcp # firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enable and start the
squidservice:systemctl enable --now squid
# systemctl enable --now squidCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To verify that the proxy works correctly, download a web page using the curl utility:
curl -O -L "https://www.redhat.com/index.html" -x "user_name:password@proxy.example.com:3128"
# 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.html file was downloaded to the current directory, the proxy works.
Troubleshooting steps
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:389
# /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:389Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enter a valid user name and password, and press Enter:
user_name password
user_name passwordCopy to Clipboard Copied! Toggle word wrap Toggle overflow If the helper utility returns
OK, authentication succeeded.
3.3. Setting up Squid as a caching proxy with kerberos authentication Copy linkLink copied to clipboard!
You can configure Squid as a caching proxy that authenticates users to an Active Directory (AD) using Kerberos. The procedure configures that only authenticated users can use the proxy.
Prerequisites
-
The procedure assumes that the
/etc/squid/squid.conffile is as provided by thesquidpackage. If you edited this file before, remove the file and reinstall the package. - The server on which you want to install Squid is a member of the AD domain.
Procedure
Install the following packages:
dnf install squid krb5-workstation
# dnf install squid krb5-workstationCopy to Clipboard Copied! Toggle word wrap Toggle overflow Authenticate as the AD domain administrator:
kinit administrator@AD.EXAMPLE.COM
# kinit administrator@AD.EXAMPLE.COMCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create 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 administrator
# export KRB5_KTNAME=FILE:/etc/squid/HTTP.keytab # net ads keytab CREATE -U administrator # net ads keytab ADD HTTP -U administratorCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: If system is initially joined to the AD domain with realm (via
adcli), use following instructions to 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 HTTP
# 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 HTTPCopy to Clipboard Copied! Toggle word wrap Toggle overflow Load the
/etc/krb5.keytabfile, remove all service principals exceptHTTP, and save the remaining principals into the/etc/squid/HTTP.keytabfile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the interactive shell of
ktutil, you can use the different options, until all unwanted principals are removed from keytab, for example:ktutil: delent 1
ktutil: delent 1Copy to Clipboard Copied! Toggle word wrap Toggle overflow Copy to Clipboard Copied! Toggle word wrap Toggle overflow WarningThe keys in
/etc/krb5.keytabmight get updated if SSSD or Samba/winbind will update the machine account password. After the update, the key in/etc/squid/HTTP.keytabwill stop working, and you will 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.keytab
# chown squid /etc/squid/HTTP.keytabCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Verify that the keytab file contains the
HTTPservice principal for the fully-qualified domain name (FQDN) of the proxy server:Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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.COM
auth_param negotiate program /usr/lib64/squid/negotiate_kerberos_auth -k /etc/squid/HTTP.keytab -s HTTP/proxy.ad.example.com@AD.EXAMPLE.COMCopy to Clipboard Copied! Toggle word wrap Toggle overflow The following describes the parameters passed to the
negotiate_kerberos_authhelper utility in the example above:-
-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 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-auth
acl kerb-auth proxy_auth REQUIRED http_access allow kerb-authCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantSpecify 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 localnet
http_access allow localnetCopy to Clipboard Copied! Toggle word wrap Toggle overflow The following ACL exists in the default configuration and defines
443as a port that uses the HTTPS protocol:acl SSL_ports port 443
acl SSL_ports port 443Copy to Clipboard Copied! Toggle word wrap Toggle overflow If 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_number
acl SSL_ports port port_numberCopy to Clipboard Copied! Toggle word wrap Toggle overflow Update 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 443
acl Safe_ports port 21 acl Safe_ports port 80 acl Safe_ports port 443Copy to Clipboard Copied! Toggle word wrap Toggle overflow By default, the configuration contains 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 256
cache_dir ufs /var/spool/squid 10000 16 256Copy to Clipboard Copied! Toggle word wrap Toggle overflow With 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
# mkdir -p path_to_cache_directoryCopy to Clipboard Copied! Toggle word wrap Toggle overflow Configure the permissions for the cache directory:
chown squid:squid path_to_cache_directory
# chown squid:squid path_to_cache_directoryCopy to Clipboard Copied! Toggle word wrap Toggle overflow If 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_directory
# semanage fcontext -a -t squid_cache_t "path_to_cache_directory(/.*)?" # restorecon -Rv path_to_cache_directoryCopy to Clipboard Copied! Toggle word wrap Toggle overflow If 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 --reload
# firewall-cmd --permanent --add-port=3128/tcp # firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enable and start the
squidservice:systemctl enable --now squid
# systemctl enable --now squidCopy to Clipboard Copied! Toggle word wrap Toggle overflow
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"
# curl -O -L "https://www.redhat.com/index.html" --proxy-negotiate -u : -x "proxy.ad.example.com:3128"Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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.COM
# kinit user@AD.EXAMPLE.COMCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Display the ticket:
klist
# klistCopy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
negotiate_kerberos_auth_testutility to test the authentication:/usr/lib64/squid/negotiate_kerberos_auth_test proxy.ad.example.com
# /usr/lib64/squid/negotiate_kerberos_auth_test proxy.ad.example.comCopy to Clipboard Copied! Toggle word wrap Toggle overflow If the helper utility returns a token, the authentication succeeded:
Token: YIIFtAYGKwYBBQUCoIIFqDC...
Token: YIIFtAYGKwYBBQUCoIIFqDC...Copy to Clipboard Copied! Toggle word wrap Toggle overflow
3.4. Configuring a domain deny list in Squid Copy linkLink copied to clipboard!
Frequently, administrators want to block access to specific domains. This section describes how to configure a domain deny list in Squid.
Prerequisites
- Squid is configured, and users can use the proxy.
Procedure
Edit the
/etc/squid/squid.conffile and add the following settings:acl domain_deny_list dstdomain "/etc/squid/domain_deny_list.txt" http_access deny all domain_deny_list
acl domain_deny_list dstdomain "/etc/squid/domain_deny_list.txt" http_access deny all domain_deny_listCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantAdd 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.net, add:.example.com example.net
.example.com example.netCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantIf 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
# systemctl restart squidCopy to Clipboard Copied! Toggle word wrap Toggle overflow
3.5. Configuring the Squid service to listen on a specific port or IP address Copy linkLink copied to clipboard!
By default, the Squid proxy service listens on the 3128 port on all network interfaces. You can change the port and configuring Squid to listen on a specific IP address.
Prerequisites
-
The
squidpackage is installed.
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, set:http_port 8080
http_port 8080Copy to Clipboard Copied! Toggle word wrap Toggle overflow To 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, set:http_port 192.0.2.1:3128
http_port 192.0.2.1:3128Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add multiple
http_portparameters to the configuration file to configure that Squid listens on multiple ports and IP addresses:http_port 192.0.2.1:3128 http_port 192.0.2.1:8080
http_port 192.0.2.1:3128 http_port 192.0.2.1:8080Copy to Clipboard Copied! Toggle word wrap Toggle overflow
If you configured that Squid uses a different port as the default (
3128):Open the port in the firewall:
firewall-cmd --permanent --add-port=port_number/tcp firewall-cmd --reload
# firewall-cmd --permanent --add-port=port_number/tcp # firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow If 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
# semanage port -a -t squid_port_t -p tcp port_numberCopy to Clipboard Copied! Toggle word wrap Toggle overflow If the
semanageutility is not available on your system, install thepolicycoreutils-python-utilspackage.
Restart the
squidservice:systemctl restart squid
# systemctl restart squidCopy to Clipboard Copied! Toggle word wrap Toggle overflow