33 Apache als Forward Proxy mit SSL/TLS-Interception und Lastverteilung

33.1 Schritt-für-Schritt-Anleitung

33.1.1 Installation des Apache-HTTP-Servers

Debian-basierte Systeme (z.B. Ubuntu):

sudo apt-get update
sudo apt-get install apache2

Red Hat-basierte Systeme (z.B. CentOS):

sudo yum update
sudo yum install httpd

33.1.2 Aktivieren der notwendigen Module

Debian-basierte Systeme:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod ssl
sudo systemctl restart apache2

Red Hat-basierte Systeme: Bearbeiten der Apache-Konfigurationsdatei (httpd.conf), um die Module zu laden:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule ssl_module modules/mod_ssl.so

Danach Apache-Dienst neu starten:

sudo systemctl restart httpd

33.1.3 Konfiguration des Forward Proxy

Bearbeiten der Datei /etc/apache2/sites-available/000-default.conf (Debian) oder /etc/httpd/conf/httpd.conf (Red Hat):

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Proxy-Einstellungen
    ProxyRequests On
    ProxyVia On

    <Proxy *>
        Require all granted
    </Proxy>

    # Logging
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

33.1.4 Konfiguration der Proxy-Module (mod_proxy, mod_ssl)

33.1.4.1 Beispielkonfiguration für HTTPS Proxy

Bearbeiten der Datei /etc/apache2/sites-available/default-ssl.conf (Debian) oder /etc/httpd/conf.d/ssl.conf (Red Hat):

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Proxy Konfiguration
        ProxyRequests On
        ProxyPass / http://backend_server/
        ProxyPassReverse / http://backend_server/

        # SSL Konfiguration
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
        SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

        <Proxy *>
            Require all granted
        </Proxy>

        # Logging
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
</IfModule>

33.1.5 Lastverteilung und Failover

Bearbeiten der Datei /etc/apache2/sites-available/000-default.conf:

<Proxy "balancer://mycluster">
    BalancerMember "http://backend1" loadfactor=1
    BalancerMember "http://backend2" loadfactor=2

    # Load Balancer Management
    ProxySet lbmethod=byrequests
</Proxy>

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Proxy Konfiguration
    ProxyPass "/balancer" "balancer://mycluster"
    ProxyPassReverse "/balancer" "balancer://mycluster"

    # Logging
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

33.1.6 Sicherheitsaspekte und Hardening

33.1.6.1 SSL/TLS-Termination und Sicherheitsrichtlinien

  1. Aktualisieren und Verwenden von starken SSL/TLS-Zertifikaten
  2. Konfigurieren von Sicherheitsheadern
  3. Beschränken des Zugriffs

33.1.7 Erstellung eines selbstsignierten Zertifikats

  1. Erstellen einer eigenen Zertifizierungsstelle (CA)

    openssl genpkey -algorithm RSA -out /etc/apache2/ssl/myCA.key -aes256
    openssl req -new -x509 -days 3650 -key /etc/apache2/ssl/myCA.key -out /etc/apache2/ssl/myCA.pem
  2. Erstellen eines privaten Schlüssels und eines Zertifikats für Apache

    openssl genpkey -algorithm RSA -out /etc/apache2/ssl/apache.key
    openssl req -new -key /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.csr
    openssl x509 -req -days 3650 -in /etc/apache2/ssl/apache.csr -CA /etc/apache2/ssl/myCA.pem -CAkey /etc/apache2/ssl/myCA.key -CAcreateserial -out /etc/apache2/ssl/apache.crt

33.1.8 Konfiguration von Apache für TLS-Interception

  1. Aktivieren der notwendigen Module

    sudo a2enmod ssl
    sudo a2enmod proxy
    sudo a2enmod proxy_http
    sudo a2enmod proxy_connect
    sudo systemctl restart apache2
  2. Bearbeiten der Apache-Konfigurationsdatei

    sudo nano /etc/apache2/sites-available/000-default.conf
  3. Hinzufügen der folgenden Konfiguration für TLS-Interception

    <VirtualHost *:3128>
        SSLEngine on
        SSLCertificateFile "/etc/apache2/ssl/apache.crt"
        SSLCertificateKeyFile "/etc/apache2/ssl/apache.key"
        SSLCACertificateFile "/etc/apache2/ssl/myCA.pem"
    
        # Proxy-Konfiguration
        ProxyRequests On
        ProxyPreserveHost On
    
        <Proxy *>
            Require all granted
        </Proxy>
    
        # SSL/TLS-Interception
        SSLProxyEngine On
        SSLProxyVerify none
        SSLProxyCheckPeerCN off
        SSLProxyCheckPeerName off
    
        ProxyPass / http://backendserver/
        ProxyPassReverse / http://backendserver/
    </VirtualHost>

33.1.9 Initialisieren und Verwalten der SSL-Datenbank

  1. Initialisieren der SSL-Datenbank

    sudo /usr/lib/apache2/security_file_certgen -c -s /var/lib/apache2/ssl_db -M 4MB

33.1.10 Neustarten des Apache-Dienstes

sudo systemctl restart apache2

33.1.11 Installieren des CA-Zertifikats auf den Clients

  1. Kopieren des CA-Zertifikats auf die Clients
  2. Installieren des CA-Zertifikats

33.2 Testen der Konfiguration

33.2.1 Verwenden von curl zum Testen des Proxies

curl -x http://<Apache-IP>:80 http://example.com

33.2.2 Überprüfen der Log-Dateien

Debian-basierte Systeme:

sudo tail -f /var/log/apache2/access.log

Red Hat-basierte Systeme:

sudo tail -f /var/log/httpd/access_log

Durch diese Schritte können Sie Apache effektiv als HTTP-Proxy einrichten und betreiben, sowohl als Forward Proxy für interne Netzwerke als auch als Reverse Proxy für Webanwendungen, um die Sicherheit, Leistung und Verfügbarkeit zu erhöhen.