Zum Hauptinhalt springen
Zurück zur Kategorie

Proxy-Server-Einrichtung: Squid und HAProxy Anleitung

Squid Forward Proxy, HAProxy Load Balancer, SSL-Terminierung, ACL-Regeln, Caching, Health Checks und Hochverfügbarkeit.

Lesezeit: 16 Min. Monitoring
proxysquidhaproxyload balancerssl terminationcaching

Inhaltsverzeichnis

Proxy-Server-Einrichtung: Squid und HAProxy Anleitung

Proxy-Server sind kritische Komponenten für die Verwaltung des Netzwerkverkehrs, die Verbesserung der Sicherheit und die Bereitstellung von Lastverteilung. In dieser Anleitung behandeln wir die Einrichtung von Squid Forward Proxy und HAProxy Load Balancer im Detail.

Was ist Squid Proxy?

Squid ist der am weitesten verbreitete Open-Source Forward Proxy und Caching-Server. Er spart Bandbreite durch Zwischenspeicherung von Webinhalten und verbessert die Netzwerksicherheit mit Zugriffskontrollregeln.

Wichtige Funktionen von Squid:

  • HTTP/HTTPS Proxy: Forward- und Reverse-Proxy-Unterstützung
  • Caching: Zwischenspeicherung von Webinhalten
  • ACL (Access Control List): Detaillierte Zugriffskontrolle
  • Bandwidth Throttling: Bandbreitenbegrenzung
  • SSL Bumping: HTTPS-Verkehrsinspektion
  • Authentifizierung: LDAP, RADIUS, NCSA-Authentifizierung

Squid Installation

Installation mit Paketmanager

hljs bash
# Ubuntu/Debian
sudo apt update
sudo apt install squid -y

# CentOS/RHEL
sudo dnf install squid -y

# Service-Status
sudo systemctl enable --now squid
sudo systemctl status squid

Squid mit Docker

hljs yaml
# docker-compose.yml
version: '3.8'
services:
  squid:
    image: ubuntu/squid:latest
    container_name: squid-proxy
    restart: unless-stopped
    ports:
      - "3128:3128"
    volumes:
      - ./squid.conf:/etc/squid/squid.conf
      - squid_cache:/var/spool/squid
      - squid_logs:/var/log/squid

volumes:
  squid_cache:
  squid_logs:

Squid Grundkonfiguration

hljs bash
# /etc/squid/squid.conf

# Netzwerkdefinitionen
acl localnet src 10.0.0.0/8
acl localnet src 172.16.0.0/12
acl localnet src 192.168.0.0/16

# Port-Definitionen
acl SSL_ports port 443
acl Safe_ports port 80
acl Safe_ports port 443
acl Safe_ports port 21
acl Safe_ports port 1025-65535
acl CONNECT method CONNECT

# Zugriffsregeln
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localhost manager
http_access deny manager
http_access allow localnet
http_access allow localhost
http_access deny all

# Proxy-Port
http_port 3128

# Cache-Einstellungen
cache_mem 256 MB
maximum_object_size_in_memory 512 KB
cache_dir ufs /var/spool/squid 10000 16 256
maximum_object_size 100 MB

# Log-Einstellungen
access_log /var/log/squid/access.log squid
cache_log /var/log/squid/cache.log

# Datenschutz
forwarded_for off
request_header_access Via deny all
request_header_access X-Forwarded-For deny all

Squid ACL-Regeln

hljs bash
# Bestimmte Websites blockieren
acl blocked_sites dstdomain .facebook.com .twitter.com .instagram.com
http_access deny blocked_sites

# Bestimmte Dateitypen blockieren
acl blocked_extensions urlpath_regex -i \.(mp3|mp4|avi|mkv)$
http_access deny blocked_extensions

# Zugriff außerhalb der Arbeitszeiten einschränken
acl work_hours time MTWHF 08:00-18:00
acl social_media dstdomain .youtube.com .reddit.com
http_access deny social_media !work_hours

# Bandbreitenbegrenzung
delay_pools 1
delay_class 1 2
delay_parameters 1 1000000/1000000 500000/500000
delay_access 1 allow localnet

Squid Authentifizierung

hljs bash
sudo apt install apache2-utils -y
sudo htpasswd -c /etc/squid/passwords benutzer1

# In squid.conf hinzufügen
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwords
auth_param basic realm Proxy-Authentifizierung
auth_param basic credentialsttl 2 hours
acl authenticated proxy_auth REQUIRED
http_access allow authenticated
http_access deny all

Was ist HAProxy?

HAProxy (High Availability Proxy) ist ein leistungsstarker TCP/HTTP Load Balancer und Proxy-Server. Er kann Millionen gleichzeitiger Verbindungen verarbeiten und bietet Lastverteilung auf Unternehmensebene.

Wichtige Funktionen von HAProxy:

  • Layer 4/7 Load Balancing: TCP- und HTTP-Lastverteilung
  • SSL-Terminierung: SSL/TLS-Verschlüsselungsterminierung
  • Health Checks: Backend-Server-Gesundheitsüberwachung
  • Sticky Sessions: Sitzungspersistenz
  • Rate Limiting: Anforderungsratenbegrenzung
  • Stats Dashboard: Echtzeit-Statistikpanel

HAProxy Installation

hljs bash
sudo apt update
sudo apt install haproxy -y
haproxy -v
sudo systemctl enable --now haproxy

HAProxy mit Docker

hljs yaml
version: '3.8'
services:
  haproxy:
    image: haproxy:latest
    container_name: haproxy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "8404:8404"
    volumes:
      - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
      - ./certs:/etc/haproxy/certs:ro

HAProxy Grundkonfiguration

hljs bash
# /etc/haproxy/haproxy.cfg

global
    log /dev/log local0
    chroot /var/lib/haproxy
    user haproxy
    group haproxy
    daemon
    ssl-default-bind-options ssl-min-ver TLSv1.2

defaults
    log     global
    mode    http
    option  httplog
    option  forwardfor
    timeout connect 5000
    timeout client  50000
    timeout server  50000

listen stats
    bind *:8404
    stats enable
    stats uri /stats
    stats refresh 10s
    stats auth admin:StarkesPasswort123

frontend http_front
    bind *:80
    redirect scheme https code 301 if !{ ssl_fc }

frontend https_front
    bind *:443 ssl crt /etc/haproxy/certs/example.com.pem
    
    acl is_api path_beg /api
    use_backend api_servers if is_api
    default_backend web_servers

backend web_servers
    balance roundrobin
    option httpchk GET /health
    http-check expect status 200
    cookie SERVERID insert indirect nocache
    
    server web1 192.168.1.10:8080 check cookie web1 weight 100
    server web2 192.168.1.11:8080 check cookie web2 weight 100

backend api_servers
    balance leastconn
    option httpchk GET /health
    
    server api1 192.168.1.20:3000 check
    server api2 192.168.1.21:3000 check

SSL-Terminierung

hljs bash
sudo cat /etc/letsencrypt/live/example.com/fullchain.pem \
     /etc/letsencrypt/live/example.com/privkey.pem \
     > /etc/haproxy/certs/example.com.pem

HAProxy Health Check Konfiguration

hljs bash
backend web_servers
    option httpchk
    http-check connect
    http-check send meth GET uri /health ver HTTP/1.1 hdr Host example.com
    http-check expect status 200
    
    server web1 192.168.1.10:8080 check inter 5s fall 3 rise 2
    server web2 192.168.1.11:8080 check inter 5s fall 3 rise 2

HAProxy Rate Limiting

hljs bash
frontend https_front
    bind *:443 ssl crt /etc/haproxy/certs/example.com.pem
    
    stick-table type ip size 100k expire 30s store http_req_rate(10s)
    http-request track-sc0 src
    http-request deny deny_status 429 if { sc_http_req_rate(0) gt 200 }

HAProxy Hochverfügbarkeit (HA)

hljs bash
sudo apt install keepalived -y

# /etc/keepalived/keepalived.conf (Master)
vrrp_script check_haproxy {
    script "/usr/bin/killall -0 haproxy"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    virtual_ipaddress {
        192.168.1.100/24
    }
    track_script {
        check_haproxy
    }
}

Konfigurationsvalidierung und Tests

hljs bash
# Squid-Konfigurationsprüfung
squid -k parse

# HAProxy-Konfigurationsprüfung
haproxy -c -f /etc/haproxy/haproxy.cfg

# Proxy-Test
curl -x http://proxy-ip:3128 http://example.com

Richten Sie auf Ihren REXE-Servern sicheren Internetzugang mit Squid und hochverfügbare Lastverteilung mit HAProxy ein. Mit SSL-Terminierung und Health-Check-Funktionen können Sie eine Infrastruktur auf Unternehmensebene aufbauen.