Saat Nginx atau Apache berada di belakang reverse proxy (misalnya Cloudflare, HAProxy, AWS ELB, atau Nginx Proxy), server hanya akan melihat alamat IP milik proxy, bukan alamat IP asli pengunjung.
Agar server dapat mengenali alamat IP asli klien, Anda perlu mengonfigurasi:
- Nginx menggunakan modul
ngx_http_realip_module - Apache menggunakan modul
mod_remoteip
Tanpa konfigurasi tersebut, server akan mencatat alamat IP proxy sehingga dapat memengaruhi berbagai fitur, seperti:
- Rate limiting
- Geo-blocking
- Analitik pengunjung
- Firewall berbasis IP
- Pencatatan (logging)
Konfigurasi Cloudflare #
Cloudflare mengirimkan alamat IP asli pengunjung melalui header CF-Connecting-IP.
cPanel / EasyApache 4 #
Instal modul mod_remoteip.
yum install -y ea-apache24-mod_remoteipBuat direktori konfigurasi berikut.
mkdir -p /etc/apache2/conf.d/userdata/ssl/2_4/username/example.com
mkdir -p /etc/apache2/conf.d/userdata/std/2_4/username/example.comSelanjutnya buat file remoteip.conf pada kedua direktori tersebut.
nano /etc/apache2/conf.d/userdata/ssl/2_4/username/example.com/remoteip.conf
nano /etc/apache2/conf.d/userdata/std/2_4/username/example.com/remoteip.confIsi file dengan konfigurasi berikut.
# Daftar IP Cloudflare:
# https://www.cloudflare.com/ips/
RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxy 173.245.48.0/20
RemoteIPTrustedProxy 103.21.244.0/22
RemoteIPTrustedProxy 103.22.200.0/22
RemoteIPTrustedProxy 103.31.4.0/22
RemoteIPTrustedProxy 141.101.64.0/18
RemoteIPTrustedProxy 108.162.192.0/18
RemoteIPTrustedProxy 190.93.240.0/20
RemoteIPTrustedProxy 188.114.96.0/20
RemoteIPTrustedProxy 197.234.240.0/22
RemoteIPTrustedProxy 198.41.128.0/17
RemoteIPTrustedProxy 162.158.0.0/15
RemoteIPTrustedProxy 104.16.0.0/13
RemoteIPTrustedProxy 104.24.0.0/14
RemoteIPTrustedProxy 172.64.0.0/13
RemoteIPTrustedProxy 131.0.72.0/22Rebuild konfigurasi Apache.
/scripts/rebuildhttpdconfApabila proses selesai tanpa error, restart Apache.
/scripts/restartsrv_httpdMengaktifkan untuk seluruh akun cPanel.
Apabila ingin mengaktifkan mod_remoteip untuk seluruh domain di server, buat file berikut.
touch /etc/apache2/conf.d/userdata/remoteip.confKemudian isi file tersebut dengan konfigurasi yang sama seperti sebelumnya, lalu jalankan kembali:
/scripts/rebuildhttpdconf
/scripts/restartsrv_httpdApache #
Buat file:
/etc/httpd/conf.d/cloudflare.confKemudian isi dengan konfigurasi berikut.
<IfModule mod_remoteip.c>
# Daftar IP Cloudflare:
# https://www.cloudflare.com/ips/
RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxy 173.245.48.0/20
RemoteIPTrustedProxy 103.21.244.0/22
RemoteIPTrustedProxy 103.22.200.0/22
RemoteIPTrustedProxy 103.31.4.0/22
RemoteIPTrustedProxy 141.101.64.0/18
RemoteIPTrustedProxy 108.162.192.0/18
RemoteIPTrustedProxy 190.93.240.0/20
RemoteIPTrustedProxy 188.114.96.0/20
RemoteIPTrustedProxy 197.234.240.0/22
RemoteIPTrustedProxy 198.41.128.0/17
RemoteIPTrustedProxy 162.158.0.0/15
RemoteIPTrustedProxy 104.16.0.0/13
RemoteIPTrustedProxy 104.24.0.0/14
RemoteIPTrustedProxy 172.64.0.0/13
RemoteIPTrustedProxy 131.0.72.0/22
</IfModule>Agar log menampilkan alamat IP asli, ubah LogFormat pada httpd.conf.
Dari:
LogFormat "%h %l %u %t \"%r\" %>s %b" commonMenjadi:
LogFormat "%a %l %u %t \"%r\" %>s %b" common%h menjadi %a pada semua konfigurasi LogFormat.
Restart Apache.
systemctl restart httpdNginx #
Buat file misalnya:
/etc/nginx/conf.d/cloudflare.confBerikut contoh skrip untuk mengunduh daftar IP Cloudflare secara otomatis.
#!/bin/bash
curl -fsSL https://www.cloudflare.com/ips-v4 > /tmp/cf_ips
curl -fsSL https://www.cloudflare.com/ips-v6 >> /tmp/cf_ips
cat > /etc/nginx/conf.d/cloudflare.conf <<EOF
real_ip_header CF-Connecting-IP;
real_ip_recursive on;
EOF
while read ip; do
echo "set_real_ip_from $ip;" >> /etc/nginx/conf.d/cloudflare.conf
done < /tmp/cf_ips
rm -f /tmp/cf_ipsPastikan nginx.conf telah memuat seluruh file konfigurasi di dalam direktori conf.d.
http {
include /etc/nginx/conf.d/*.conf;
# konfigurasi lainnya
}Tambahkan cron job agar daftar IP Cloudflare diperbarui secara berkala.
0 0 * * 0 /path/to/cloudflare-ip-update.sh && nginx -s reloadKonfigurasi Reverse Proxy Umum #
Untuk reverse proxy selain Cloudflare (misalnya HAProxy, AWS ELB, atau Nginx Proxy), alamat IP asli biasanya dikirim melalui header X-Forwarded-For atau X-Real-IP.
Apache #
Buat file:
/etc/httpd/conf.d/remoteip.confIsi dengan konfigurasi berikut.
<IfModule mod_remoteip.c>
RemoteIPHeader X-Forwarded-For
# Ganti dengan alamat IP atau jaringan milik proxy Anda
RemoteIPTrustedProxy 127.0.0.1
</IfModule>Nginx #
Buat file:
/etc/nginx/conf.d/remoteip.confTambahkan konfigurasi berikut di dalam blok http.
http {
# IP atau jaringan milik reverse proxy
set_real_ip_from 203.0.113.5;
set_real_ip_from 192.168.1.0/24;
# Header yang membawa IP asli
real_ip_header X-Forwarded-For;
# Gunakan IP terakhir yang bukan berasal dari proxy tepercaya
real_ip_recursive on;
}set_real_ip_from 0.0.0.0/0 atau mempercayai seluruh alamat IP. Selalu batasi hanya pada alamat IP atau rentang CIDR milik reverse proxy yang Anda gunakan untuk mencegah pemalsuan (IP spoofing).