Skip to main content
๐Ÿš€ nusa.id cloud server linux indonesialihat paket

Konfigurasi Apache sebagai Reverse Proxy

ยท 4 min read
Aidil Putra
Maintainer

Reverse proxy adalah server yang berdiri di depan aplikasi backend dan meneruskan permintaan klien ke server tujuan. Apache dengan modul mod_proxy dapat berfungsi sebagai reverse proxy yang andal untuk aplikasi seperti Node.js, Python (Gunicorn), atau container Docker.

Panduan ini mencakup instalasi dan konfigurasi Apache reverse proxy di server Nusa Cloud VPS berbasis Ubuntu 24.04 atau Debian 12.


Prasyaratโ€‹

  • Server Nusa Cloud VPS dengan OS Ubuntu 24.04 atau Debian 12
  • Akses root atau user dengan hak sudo
  • Aplikasi backend sudah berjalan di port tertentu (misal localhost:3000 atau 127.0.0.1:8000)

Langkah 1 โ€” Install Apacheโ€‹

sudo apt update
sudo apt install -y apache2

Pastikan Apache berjalan:

sudo systemctl status apache2

Langkah 2 โ€” Aktifkan Modul Proxyโ€‹

Apache membutuhkan beberapa modul untuk berfungsi sebagai reverse proxy:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
sudo a2enmod headers
sudo a2enmod rewrite

Restart Apache untuk menerapkan modul:

sudo systemctl restart apache2

Langkah 3 โ€” Konfigurasi Virtual Hostโ€‹

Buat file konfigurasi virtual host baru:

sudo nano /etc/apache2/sites-available/app-reverse-proxy.conf

Isi dengan konfigurasi berikut โ€” sesuaikan ServerName, ProxyPass, dan ProxyPassReverse:

<VirtualHost *:80>
ServerName app.namadomain.com

# Forward semua permintaan ke aplikasi backend
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/

# Header untuk informasi klien asli
ProxyAddHeaders On
RequestHeader set X-Forwarded-Proto "http"
RequestHeader set X-Forwarded-Port "80"

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

Aktifkan virtual host:

sudo a2ensite app-reverse-proxy.conf
sudo systemctl reload apache2

Penjelasan Konfigurasiโ€‹

DirectiveFungsi
ProxyPreserveHost OnMeneruskan header Host asli dari klien
ProxyPass / http://127.0.0.1:3000/Meneruskan semua request ke backend
ProxyPassReverse / http://127.0.0.1:3000/Menulis ulang header redirect dari backend
RequestHeader set X-Forwarded-*Memberi tahu backend tentang IP/port/protocol asli klien

Langkah 4 โ€” Konfigurasi untuk WebSocketโ€‹

Jika aplikasi backend menggunakan WebSocket (seperti Node.js dengan Socket.io), tambahkan konfigurasi khusus:

<VirtualHost *:80>
ServerName app.namadomain.com

ProxyPreserveHost On

# WebSocket proxy
ProxyPass /ws ws://127.0.0.1:3000/ws
ProxyPassReverse /ws ws://127.0.0.1:3000/ws

# HTTP proxy
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/

ErrorLog ${APACHE_LOG_DIR}/app-error.log
CustomLog ${APACHE_LOG_DIR}/app-access.log combined
</VirtualHost>

Aktifkan modul WebSocket proxy:

sudo a2enmod proxy_wstunnel
sudo systemctl reload apache2

Langkah 5 โ€” Tambahkan SSL (HTTPS)โ€‹

Untuk produksi, tambahkan SSL menggunakan Certbot:

sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d app.namadomain.com

Setelah SSL aktif, konfigurasi virtual host akan secara otomatis diubah oleh Certbot. Konfigurasi proxy akan tetap berfungsi di dalam blok <VirtualHost *:443>.

โš ๏ธ Warning: Pastikan aplikasi backend dapat menerima header X-Forwarded-Proto: https agar tautan yang dihasilkan menggunakan HTTPS.

Konfigurasi SSL Manualโ€‹

Jika ingin mengatur SSL secara manual, tambahkan konfigurasi berikut:

<VirtualHost *:443>
ServerName app.namadomain.com

SSLEngine On
SSLCertificateFile /etc/letsencrypt/live/app.namadomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/app.namadomain.com/privkey.pem

ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/

RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "443"

ErrorLog ${APACHE_LOG_DIR}/app-ssl-error.log
CustomLog ${APACHE_LOG_DIR}/app-ssl-access.log combined
</VirtualHost>

Verifikasiโ€‹

  1. Pastikan aplikasi backend berjalan di port yang dituju:

    curl http://127.0.0.1:3000
  2. Akses domain dari browser: http://app.namadomain.com

  3. Periksa header response untuk memastikan proxy berfungsi:

    curl -I http://app.namadomain.com
  4. Periksa log akses:

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

Rollbackโ€‹

Untuk menonaktifkan reverse proxy dan mengembalikan konfigurasi Apache seperti semula:

sudo a2dissite app-reverse-proxy.conf
sudo systemctl reload apache2

Hapus file konfigurasi jika tidak diperlukan lagi:

sudo rm /etc/apache2/sites-available/app-reverse-proxy.conf

Kesimpulanโ€‹

Apache reverse proxy memungkinkan Anda menjalankan berbagai aplikasi backend di belakang satu web server yang stabil. Dengan mod_proxy, Apache dapat menangani routing, load balancing, SSL termination, dan caching untuk aplikasi Node.js, Python, Ruby, atau container Docker.

Langkah selanjutnya:

  • Tambahkan load balancing dengan ProxyBalancer untuk beberapa instance backend
  • Gunakan CacheEnable untuk caching konten statis
  • Integrasikan dengan Nusa Cloud VPS untuk deployment aplikasi produksi

Referensiโ€‹