DEV Community

Fega Suseno
Fega Suseno

Posted on • Updated on

Reverse Proxy Nginx Ubuntu 20.04

Requirement

  • Web Server Running (Apache2 on IP 10.10.10.1:80)
  • Reverse Proxy (Nginx on IP 10.10.10.2)

Assumption

Di asumsikan sudah ada web server yng berjalan. Pada contoh disini web server berjalan pada IP 10.10.10.1:80.
Reverse Proxy Server akan di jalankan di VM atau Server yg berbeda dengan IP 10.10.10.2.

Installation

Install Nginx

apt install nginx
Enter fullscreen mode Exit fullscreen mode

Unlink default config nginx

unlink /etc/nginx/sites-enabled/default
Enter fullscreen mode Exit fullscreen mode

Buat file reverse-proxy.conf pada /etc/nginx/sites-availible

touch /etc/nginx/sites-available/reverse-proxy.conf
Enter fullscreen mode Exit fullscreen mode

isikan dengan konfigurasi berikut:

server {
    listen 80;
   # listen 443 ssl;
    server_name www.domain.com;
    access_log /var/log/nginx/reverse-proxy-access.log;
    error_log /var/log/nginx/reverse-proxy-error.log;
   # ssl on;
       # include snippets/ssl-defaults.conf;
       # include snippets/proxy-defaults.conf;

    location / {
        #Set header Proxynya
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Accept-Encoding "";

         #Tujuan permintan diteruskan
        proxy_pass http://10.10.10.1/;
    }
}

Enter fullscreen mode Exit fullscreen mode

Kemudian buat permalink file reverse-proxy.conf pada sites-enabled.

ln /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf
Enter fullscreen mode Exit fullscreen mode

Cek apakah konfigurasinya sudah benar

nginx -t
Enter fullscreen mode Exit fullscreen mode

Restart nginx dan set enable agar autostart

systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode
systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

Lakukan uji coba dengan mengakses IP server reverse proxy, jika sesuai harusnya yg akan tampil adalah web server pada server 1.
Selamat mencoba

Top comments (0)