setup apache2 with reverse proxy

consoleGuyconsoleGuy
3 min read

biasanya bila aku develop system backend kita perlu kan frontend untuk mapping data atau query dari end user. so masa proses communication backend via frontend ni selalunya hacker akan cuba exploit atau buat finger printing nak probe info apa yang kau guna kat backend contohnya info version db,OS, WAF etc etc (guna kan imaginasi dan creativity )

sebagai contoh ada orang pakai expressjs,, nodejs kastrel server di backend. so ni bagi aku tak sesuai untuk expose directly di internet.

jadi kat sini aku akan pakai apache atau nginx(nginx ni best, boleh custom macam2)

jom aku share cara aku setup apache2 reverse proxy. di sebab kan ramai kat sini familiar dengan debian based, aku akan setup guna debian (bookworm). dalam contoh ni aku cuma setup http. untuk https maybe bila aku lapang aku sambung di thread yang lain.

big bonus pakai cara ni ialah , kau boleh custom WAF, guna modsecurity dari OWASP https://github.com/owasp-modsecurity/ModSecurity.

agak2 kalau pakai ni lepas tu ada lagi WAF dari cloudflare, double WAF? mantap pak abu, maybe ada performance issue sebab ada firewall process banyak, plus ada lagi fail2ban module dalam modsecurity ni. menarik bro,

aku guna dotnet , install dotnet dulu. kat sini korang boleh install SDK yang sesuai dengan apps masing2

Code:

apt-get update

install SDK .NET 8

Code:

apt-get install -y dotnet-sdk-8.0

verify SDK

Code:

sky@rc1:~$ dotnet --info
.NET SDK:
 Version:           8.0.401
 Commit:            811edcc344
 Workload version:  8.0.400-manifests.b6724b7a
 MSBuild version:   17.11.4+37eb419ad

Runtime Environment:
 OS Name:     debian
 OS Version:  12
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /usr/share/dotnet/sdk/8.0.401/

.NET workloads installed:
Configured to use loose manifests when installing new manifests.
There are no installed workloads to display.

Host:
  Version:      8.0.8
  Architecture: x64
  Commit:       08338fcaa5

.NET SDKs installed:
  8.0.401 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.8 [/usr/share/dotnet/shared/Microsoft.AspNetCore.                                                        App]
  Microsoft.NETCore.App 8.0.8 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download
sky@rc1:~$

install apache2

Code:

apt install apache2

lepas aku siap install apache2 , aku perlu stop service apache2 , untuk load modules proxy

Code:

systemctl apache2 stop

load modules yang di perlukan untuk reverse proxy

Code:

a2enmod headers
a2enmod proxy
a2enmod proxy_http
a2enmod headers
a2enmod proxy
a2enmod proxy_http
a2enmod remoteip

modules remoteip kegunaan dia ialah untuk detect IP visitor yang masuk dalam system webapp kau. ni nanti aku akan explain kat apache2 vhost config file

create vhost

Code:

vi /etc/apache2/sites-available/example.com

contoh running vhost config, vhost ni virtual host so kalau kau ada 100 domain name pun kau boleh run kat dalam server yang sama

Code:

<VirtualHost *:80>
    ProxyRequests Off

    <Proxy *>
      Order deny,allow
      Allow from all
    </Proxy>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ServerName  example.com
    ServerAlias *.example.com

    RemoteIPHeader X-Forwarded-For

    LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    CustomLog ${APACHE_LOG_DIR}/example.log combined
    ErrorLog ${APACHE_LOG_DIR}/example_err.log
</VirtualHost>

line ni maksudnya dia kan log ip visitor kau

LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

lastly, aku create 1 service file dalam systemd /etc/systemd/system/example.service . ni kau boleh google sendiri lah

good luck happy coding

freebsd rules!

0
Subscribe to my newsletter

Read articles from consoleGuy directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

consoleGuy
consoleGuy