Menggunakan Apache Sebagai Proxy dan Load Balancer

Load balancing adalah proses mendistribusikan lalu lintas di beberapa server untuk ketersediaan tinggi (HA) dan skalabilitas elastis.

Umumnya HAProxy sering digunakan sebagai proxy dan load balancer tetapi ada alternatif lain dengan menggunakan modul proxy dari webserver Apache Anda sehingga tidak perlu menginstall HAProxy.

Activating The Modules

Aktifkan beberapa modul berikut.

a2enmod proxy proxy_ajp proxy_http rewrite deflate headers proxy_balancer proxy_connect proxy_html lbmethod_byrequests

Enabling Proxy

Edit atau buat file baru untuk konfigurasi proxy

nano /etc/apache2/sites-available/web.conf
<VirtualHost *:80>
        ProxyPreserveHost On
        
        # Servers to proxy the connection, or;
        # List of application servers:
        # Usage:
        # ProxyPass / http://[IP Addr.]:[port]/
        # ProxyPassReverse / http://[IP Addr.]:[port]/
        # Example: 
        ProxyPass / http://0.0.0.0:8080/
        ProxyPassReverse / http://0.0.0.0:8080/
        
        ServerName localhost
    </VirtualHost>

Enabling Load-Balancing

Untuk menggunakan fitur load balancing Apache

    <Proxy balancer://mycluster>
        # Server 1
        BalancerMember http://127.0.0.1:8080

        # Server 2
        BalancerMember http://127.0.0.1:8081
    </Proxy>

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName yourserver.com
    ErrorLog "/var/log/apache2/web1-error_log"
    CustomLog "/var/log/apache2/web1-access_log" common
    ProxyPass / balancer://mycluster/
</VirtualHost>

Sesuaikan kembali dengan lingkungan backend atau aplikasi yang Anda jalankan.

Restarting Apache

Setelah dikonfigurasi Anda perlu restart service Apache perubahan dapat diterapkan di server.

systemctl restart apache2