Apache httpd
Proxy

Lukáš Bařinka

© 2021

REV 2.10

TOC

Obsah

  1. Proxy usage
  2. Basic proxy configuration
  3. Load-balancing and cache
  1. Použití proxy
  2. Základní konfigurace proxy
  3. Load-balancing a cache

Proxy usage

Použití proxy

Why to use proxy

Důvody pro použití proxy

  • Bypassing
    • In case the real client can't connect to server directly
  • Caching
    • To reduce response delay
    • To reduce used bandwidth
  • Content modifying
    • Adding/removing adds
    • Filtering
  • Monitoring
    • Requests logging
  • Access controlling
    • To reduce working hours wasting
    • To limit access to certain servers
    • To authorize network access
  • Redirecting
    • Requests substitution
    • Anonymization
  • Zastupování
    • Zpřístupnění nesměrovatelných adres
  • Odkládání
    • Zrychlení přístupových dob
    • Snížení použité šířky pásma
  • Modifikace obsahu
    • Přidání reklamy
    • Filtrování
  • Monitorování
    • Monitorování využití Internetu
  • Řízení přístupu
    • Omezení zneužívání pracovní doby
    • Omezení přístupu na určené servery
    • Autorizace přístupu
  • Přesměrování
    • Nahrazení požadavků
    • Anonymizér

Proxy types

Typy proxy

  • Forward proxy
    1. User/client sends request to proxy server instead of origin server
    2. Proxy server gets requested response from origin server
    3. Proxy server sends origin server response to client/user
    1. Uživatel předá požadavek na hostitele proxy serveru
    2. Proxy server získá zamýšlenou odpověď od hostitele
    3. Proxy server předá odpověď uživateli
  • Reverse proxy
    1. Proxy server gets request from user/client User/client does not know that the server is not an origin server
    2. Proxy server serves the request from cache or gets resource from origin server
    1. Proxy serveru přijde požadavek Žadatel neví, že jde o proxy server, myslí, že žádá přímo zdroj
    2. Proxy server obslouží uživatele z paměti, nebo požádá hostitele o odpověď
  • Transparent proxy (gateway)
    • User/client sends request to server through gateway that acts like proxy server
    • Uživatel odešle požadavek na server skrz gateway, která se chová jako proxy

Basic configuration

Základní konfigurace

Forward Proxy

  • ProxyRequests Enables/disables proxy service Aktivuje nebo deaktivuje službu proxy
  • ProxyVia Enables/disables HTTP header Via Slouží k modifikaci HTTP hlavičky Via
    
    									ProxyVia On|Off|Full|Block
    								
  • NoProxy Sets (direct) access to resources without proxy usage Nastavuje přístup bez použití proxy
    
    									ProxyRemote  "*"  "http://firewall.example.com:81"
    									NoProxy         ".example.com" "192.168.112.0/21"
    								
  • ProxyBlock Words, hosts, or domains that are banned Blokuje přístup ke specifikovanému hostiteli/doméně
    
    									ProxyBlock sputniknews.com
    								
  • ProxyDomain Default domain name for proxied requests Názvy, které se nepodaří vyhodnotit se doplní o ProxyDomain
  • <Proxy> Container for directives applied to proxied resources Kontejner pro omezení platnosti direktiv na proxy server

Forward proxy configuration

Konfigurace forward proxy


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

							ProxyRequests On
							ProxyVia On

							<Proxy *>
							  Require ip 192.168.1
							</Proxy>
						
  • Access control is possible also using mod_auth
  • Zabezpečení přístupu je možné také pomocí mod_auth

Reverse proxy

  • ProxyPass directive Maps remote servers into the local server URL-space
    
    									ProxyPass [path] !|url [key=value key=value …]]
    								
    • ! does not use reverse proxy for specified path
    • Rules are checked in the order of configuration
    • Do not mix ProxyPass directives using path and ProxyPass directives inside <Location> container
    Context: server, virtualhost, <Location> The ProxyRequests directive should usually be set off
  • ProxyPass Mapuje cizí servery do místního URL
    
    									ProxyPass [path] !|url [key=value key=value …]]
    								
    • ! zabraňuje použití proxy pro danou cestu
    • Cesty se vyhodnocují podle pořadí v konfiguraci
    • Nekombinujte direktivy ProxyPass s cestami (path) a uvnitř kontejneru <Location> (bez cesty)
    Kontext: server, virtualhost, <Location> Direktiva ProxyRequests by většinou měla být vypnuta (off)
  • ProxyPassReverse directive Adjusts the URL in HTTP response headers sent from a reverse proxied server
    
    									ProxyPassReverse [path] url
    								
    • Change only HTTP response headers: Location, Content-Location, URI
    • Does not change (HTML) content addresses
    Context: server, virtualhost, <Location>
    
    									ProxyPass                    "/mirror/foo/" "http://backend.example.com/"
    									ProxyPassReverse             "/mirror/foo/" "http://backend.example.com/"
    									ProxyPassReverseCookieDomain  "backend.example.com"  "public.example.com"
    									ProxyPassReverseCookiePath   "/"  "/mirror/foo/"
    								
  • ProxyPassReverse Upravuje URL v hlavičkách odpovědí
    
    									ProxyPassReverse [path] url
    								
    • Mění pouze HTTP hlavičky odpovědi: Location, Content-Location, URI
    • Nemění adresy uvnitř (HTML) obsahu odpovědi
    Kontext: server, virtualhost, <Location>
    
    									ProxyPass                    "/mirror/foo/" "http://backend.example.com/"
    									ProxyPassReverse             "/mirror/foo/" "http://backend.example.com/"
    									ProxyPassReverseCookieDomain  "backend.example.com"  "public.example.com"
    									ProxyPassReverseCookiePath   "/"  "/mirror/foo/"
    								

Reverse proxy example

Ukázka konfigurace

  • Configuration using path
    
    									ProxyRequests Off
    									ProxyPass        /oc1 http://www.othercorp1.em
    									ProxyPassReverse /oc1 http://www.othercorp1.em
    									ProxyPassMatch ^(/.*\.jpg)$ http://images.server.com$1
    								
  • Configuration inside <Location> container Without path, that is taken from Location
    
    									ProxyRequests Off
    
    									<Location "/mirror/foo/">
    									  ProxyPass "http://backend.example.com/"
    									</Location>
    									<Location "/mirror/foo/i">
    									  ProxyPass "!"
    									</Location>
    								
    Only single ProxyPass directive can be inside <Location> container
  • Use ProxyPassMatch instead of <LocationMatch> for Regular Expressions
  • Konfigurace s použitím path
    
    									ProxyRequests Off
    									ProxyPass        /oc1 http://www.othercorp1.em
    									ProxyPassReverse /oc1 http://www.othercorp1.em
    									ProxyPassMatch ^(/.*\.jpg)$ http://images.server.com$1
    								
  • Konfigurace uvnitř kontejneru <Location> Bez specifikace cesty (path), která se vezme z Location
    
    									ProxyRequests Off
    
    									<Location "/mirror/foo/">
    									  ProxyPass "http://backend.example.com/"
    									</Location>
    									<Location "/mirror/foo/i">
    									  ProxyPass "!"
    									</Location>
    								
    Pouze jediná direktiva ProxyPass může být uvnitř kontejneru <Location>
  • Použijte direktivu ProxyPassMatch místo <LocationMatch> (nefunguje) pro RE

SSL tunneling

Tunelování SSL

  • Mainly used to tunnel SSL requests through proxy servers
  • ProxyRemote Remote proxy used to handle certain proxy requests (proxy behind proxy)
    
    									ProxyRemote match remote-server
    								
    Context: server, virtualhost
  • AllowCONNECT Ports that are allowed to CONNECT through the proxy
    
    									AllowCONNECT port[-port] [port[-port]] …
    								
    Default: AllowCONNECT 443 563, context: server, virtualhost
  • Používá se pro posílání SSL požadavků skrz proxy server
  • ProxyRemote Vzdálená proxy k obsluze některých proxy požadavků (proxy za proxy)
    
    									ProxyRemote match remote-server
    								
    Kontext: server, virtualhost
  • AllowCONNECT Rozsahy portů, které umožňují CONNECT skrz proxy
    
    									AllowCONNECT port[-port] [port[-port]] …
    								
    Default: AllowCONNECT 443 563, kontext: server, virtualhost

							a2enmod proxy_connect
						

							ProxyRequests On
							SSLProxyEngine On
							ProxyRemote * http://172.16.3.3:3128/
						

Load-balancer and cache

Load-balancer a cache

Load balancing

Vyvažování zátěže

  • To distribute workload and to increase availability (mod_proxy_balancer)
  • Load-balancing algorithms:
    • Request Counting (mod_lbmethod_byrequests)
    • Weighted Traffic Counting (mod_lbmethod_bytraffic)
    • Pending Request Counting (mod_lbmethod_bybusyness)
    • Heartbeat Traffic Counting (mod_lbmethod_heartbeat)
  • Created cluster of nodes (workers) can serve as the target for reverse proxy configuration
  • Supports "sticky sessions"
  • Slouží pro rozložení zátěže a zvýšení dostupnosti (mod_proxy_balancer)
  • Algoritmy pro plánování rozložení zátěže
    • Request Counting (mod_lbmethod_byrequests)
    • Weighted Traffic Counting (mod_lbmethod_bytraffic)
    • Pending Request Counting (mod_lbmethod_bybusyness)
    • Heartbeat Traffic Counting (mod_lbmethod_heartbeat)
  • Vytvoření clusteru a směrování požadavků na reverzní proxy server do tohoto clusteru
  • Umožňuje vytvářet "sticky session"

Blancer Manager

  • Depends on mod_status module
  • Provides balancer status check and configuration changes in runtime
  • Závisí na modulu mod_status
  • Umožňuje kontrolovat stav a měnit parametry balanceru za běhu

							<Location "/balancer-manager">
							  SetHandler balancer-manager
							  Require host localhost
							  ProxyPass "!"
							</Location>
						

http://httpd.apache.org/docs/2.4/howto/reverse_proxy.html#manager | http://httpd.apache.org/docs/2.4/images/bal-man-w.png

Load-balancing directives

Direktivy pro Load-balancing

  • BalancerMember directive Adds a member to a load-balancing group
    
    									BalancerMember [balancerurl] url [key=value [key=value ...]]
    								
    Context: directory
    • Parameter: balancerurl is only needed when not within a <Proxy balancer://>
    • Key: loadfactor a decimal number which defines the weighted load <1,100>
  • BalancerMember Přidává člena do clusteru
    
    									BalancerMember [balancerurl] url [key=value [key=value ...]]
    								
    Kontext: directory
    • Parametr: balancerurl se nenastavuje/ignoruje, pokud je konfigurace uvnitř <Proxy balancer://>
    • Klíč: loadfactor Nastavuje relativní zátěž daného člena <1,100>

Load-balancing configuration

Konfigurace load-balancingu


							<Proxy balancer://hotcluster>
								  BalancerMember http://www1.mycorp.net:80 loadfactor=1
								  BalancerMember http://www2.mycorp.net:80 loadfactor=2
								  ProxySet lbmethod=bytraffic
							</Proxy>

							ProxyPass / balancer://hotcluster/

							<Location "/balancer-manager">
							  SetHandler balancer-manager
							  Require host localhost
							  ProxyPass "!"
							</Location>
						

It is important to exclude Location /balancer-manager from proxy processing.

Location /balancer-manager podléhá také proxy, proto je potřeba proxy pro tuto adresu zakázat.


							a2enmod proxy_balancer → slotmem_shm
							a2enmod lbmethod_bytraffic
						

Proxy caching

Odkládání (caching)

  • mod_cache module Content caching uses other modules featuresStorage management modules
  • mod_cache_disk module Disk storage for cache
    
    									CacheEnable cache_type url-string
    								
  • mod_cache Odkládání obsahu využívá služeb dalších modulů (storage management modules)
  • mod_cache_disk Odkládání na disk
    
    									CacheEnable cache_type url-string
    								

Disk cache configuration

Konfigurace disk cache


							<IfModule mod_cache_disk.c>
							  CacheRoot /var/cache/apache2/mod_disk_cache
							  CacheEnable disk /
							  CacheDirLevels 5
							  CacheDirLength 3
							  CacheMaxFileSize 65536
							  CacheMinFileSize 1024
							</IfModule>