Ana içeriğe geç
Kategoriye Dön

Proxy Sunucu Kurulumu: Squid ve HAProxy Rehberi

Squid forward proxy, HAProxy load balancer, SSL termination, ACL kuralları, caching, health check ve yüksek erişilebilirlik.

Okuma süresi: 16 dk Monitoring
proxysquidhaproxyload balancerssl terminationcaching

İçindekiler

Proxy Sunucu Kurulumu: Squid ve HAProxy Rehberi

Proxy sunucular, ağ trafiğini yönetmek, güvenliği artırmak ve yük dengeleme sağlamak için kritik bileşenlerdir. Bu rehberde Squid forward proxy ve HAProxy load balancer kurulumunu detaylı olarak ele alacağız.

Squid Proxy Nedir?

Squid, en yaygın kullanılan açık kaynaklı forward proxy ve caching sunucusudur. Web trafiğini önbelleğe alarak bant genişliği tasarrufu sağlar ve erişim kontrolü kurallarıyla ağ güvenliğini artırır.

Squid'in temel özellikleri:

  • HTTP/HTTPS Proxy: Forward ve reverse proxy desteği
  • Caching: Web içeriklerini önbelleğe alma
  • ACL (Access Control List): Detaylı erişim kontrolü
  • Bandwidth Throttling: Bant genişliği sınırlama
  • SSL Bumping: HTTPS trafiğini inceleme
  • Authentication: LDAP, RADIUS, NCSA kimlik doğrulama

Squid Kurulumu

Paket Yöneticisi ile Kurulum

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

# CentOS/RHEL
sudo dnf install squid -y

# Servis durumu
sudo systemctl enable --now squid
sudo systemctl status squid

Docker ile Squid Kurulumu

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 Temel Yapılandırması

hljs bash
# /etc/squid/squid.conf

# Ağ tanımları
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 tanımları
acl SSL_ports port 443
acl Safe_ports port 80          # HTTP
acl Safe_ports port 443         # HTTPS
acl Safe_ports port 21          # FTP
acl Safe_ports port 1025-65535  # Yüksek portlar
acl CONNECT method CONNECT

# Erişim kuralları
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 portu
http_port 3128

# Cache ayarları
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
minimum_object_size 0 KB

# Log ayarları
access_log /var/log/squid/access.log squid
cache_log /var/log/squid/cache.log

# DNS ayarları
dns_nameservers 8.8.8.8 8.8.4.4

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

Squid ACL Kuralları

hljs bash
# Belirli siteleri engelle
acl blocked_sites dstdomain .facebook.com .twitter.com .instagram.com
http_access deny blocked_sites

# Belirli dosya türlerini engelle
acl blocked_extensions urlpath_regex -i \.(mp3|mp4|avi|mkv)$
http_access deny blocked_extensions

# Çalışma saatleri dışında erişimi kısıtla
acl work_hours time MTWHF 08:00-18:00
acl social_media dstdomain .youtube.com .reddit.com
http_access deny social_media !work_hours

# IP bazlı erişim
acl admin_ips src 192.168.1.100 192.168.1.101
http_access allow admin_ips

# Bant genişliği sınırlama
delay_pools 1
delay_class 1 2
delay_parameters 1 1000000/1000000 500000/500000
delay_access 1 allow localnet

Squid Kimlik Doğrulama

hljs bash
# NCSA (htpasswd) kimlik doğrulama
sudo apt install apache2-utils -y
sudo htpasswd -c /etc/squid/passwords kullanici1
sudo htpasswd /etc/squid/passwords kullanici2

# squid.conf'a ekle
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwords
auth_param basic realm Proxy Kimlik Dogrulama
auth_param basic credentialsttl 2 hours
acl authenticated proxy_auth REQUIRED
http_access allow authenticated
http_access deny all

HAProxy Nedir?

HAProxy (High Availability Proxy), yüksek performanslı bir TCP/HTTP load balancer ve proxy sunucusudur. Milyonlarca eşzamanlı bağlantıyı yönetebilir ve kurumsal düzeyde yük dengeleme sağlar.

HAProxy'nin temel özellikleri:

  • Layer 4/7 Load Balancing: TCP ve HTTP düzeyinde yük dengeleme
  • SSL Termination: SSL/TLS şifreleme sonlandırma
  • Health Checks: Backend sunucu sağlık kontrolü
  • Sticky Sessions: Oturum yapışkanlığı
  • Rate Limiting: İstek hızı sınırlama
  • Stats Dashboard: Gerçek zamanlı istatistik paneli

HAProxy Kurulumu

Paket Yöneticisi ile Kurulum

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

# Versiyon kontrolü
haproxy -v

# Servis başlat
sudo systemctl enable --now haproxy

Docker ile HAProxy

hljs yaml
# docker-compose.yml
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
    sysctls:
      - net.ipv4.ip_unprivileged_port_start=0

HAProxy Temel Yapılandırması

hljs bash
# /etc/haproxy/haproxy.cfg

global
    log /dev/log local0
    log /dev/log local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

    # SSL ayarları
    ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256
    ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384
    ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
    tune.ssl.default-dh-param 2048

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    option  forwardfor
    option  http-server-close
    timeout connect 5000
    timeout client  50000
    timeout server  50000
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http

# İstatistik paneli
listen stats
    bind *:8404
    stats enable
    stats uri /stats
    stats refresh 10s
    stats admin if LOCALHOST
    stats auth admin:GucluSifre123

# HTTP → HTTPS yönlendirme
frontend http_front
    bind *:80
    redirect scheme https code 301 if !{ ssl_fc }

# HTTPS frontend
frontend https_front
    bind *:443 ssl crt /etc/haproxy/certs/example.com.pem
    
    # ACL kuralları
    acl is_api path_beg /api
    acl is_static path_end .css .js .png .jpg .gif .ico
    
    # Backend yönlendirme
    use_backend api_servers if is_api
    use_backend static_servers if is_static
    default_backend web_servers

# Web sunucuları backend
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
    server web3 192.168.1.12:8080 check cookie web3 weight 50 backup

# API sunucuları backend
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

# Statik dosya sunucuları
backend static_servers
    balance uri
    server static1 192.168.1.30:80 check
    server static2 192.168.1.31:80 check

SSL Termination

hljs bash
# Let's Encrypt sertifikası ile HAProxy
# PEM dosyası oluştur (cert + key birleşik)
sudo cat /etc/letsencrypt/live/example.com/fullchain.pem \
     /etc/letsencrypt/live/example.com/privkey.pem \
     > /etc/haproxy/certs/example.com.pem

# Sertifika yenileme hook'u
sudo tee /etc/letsencrypt/renewal-hooks/post/haproxy.sh > /dev/null <<'EOF'
#!/bin/bash
cat /etc/letsencrypt/live/example.com/fullchain.pem \
    /etc/letsencrypt/live/example.com/privkey.pem \
    > /etc/haproxy/certs/example.com.pem
systemctl reload haproxy
EOF
sudo chmod +x /etc/letsencrypt/renewal-hooks/post/haproxy.sh

HAProxy Health Check Yapılandırması

hljs bash
# Gelişmiş health check
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
    
    # Health check aralıkları
    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
    
    # Rate limiting - IP başına saniyede 20 istek
    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 }
    
    # DDoS koruması
    stick-table type ip size 100k expire 30s store conn_cur
    http-request deny deny_status 429 if { src_conn_cur ge 50 }

HAProxy Yüksek Erişilebilirlik (HA)

Keepalived ile HAProxy HA kurulumu:

hljs bash
# Keepalived kurulumu
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
    advert_int 1
    
    authentication {
        auth_type PASS
        auth_pass GucluSifre
    }
    
    virtual_ipaddress {
        192.168.1.100/24
    }
    
    track_script {
        check_haproxy
    }
}

Yapılandırma Doğrulama ve Test

hljs bash
# Squid yapılandırma kontrolü
squid -k parse
squid -k reconfigure

# HAProxy yapılandırma kontrolü
haproxy -c -f /etc/haproxy/haproxy.cfg

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

# HAProxy stats
curl http://localhost:8404/stats

# HAProxy log izleme
tail -f /var/log/haproxy.log

REXE sunucularınızda Squid ile güvenli internet erişimi ve HAProxy ile yüksek erişilebilir yük dengeleme kurabilirsiniz. SSL termination ve health check özellikleri ile kurumsal düzeyde altyapı oluşturabilirsiniz.