loader image
Apache

How To Use Apache As Reverse Proxy

Using Apache HTTP Server as a reverse proxy allows it to forward client requests to other servers and deliver the responses back to the clients. This is useful for load balancing, isolating backend services, or hiding server details.

LAMP Script with Copy Button

For RPM/CentOS-based Systems

Script copied
[root@testing ~]# sudo yum install httpd
Script copied
[root@testing ~]# cat /etc/httpd/conf.modules.d/00-base.conf | grep LoadModule
Apache Proxy Modules
LAMP Script with Copy Button

This screenshot is to make sure that the following modules are available in your OS for reverse proxy

Script copied
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so

Create a reverse proxy conf file

Script copied
[root@testing ~]# sudo nano /etc/httpd/conf.d/reverse-proxy.conf

Copy this content and add it in the above .conf file

Note: In this Port: 8080 is where the custom application is running
Script copied
<VirtualHost *:80>

    ServerName application.domain.com

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

    ErrorLog /var/log/httpd/application_http_error.log
    CustomLog /var/log/httpd/application_http_access.log combined
    
</VirtualHost>

# HTTPS (Port 443) Configuration

<VirtualHost *:443>
    ServerName application.domain.com

    SSLEngine on
    SSLCertificateFile /etc/ssl/application/ssl.crt
    SSLCertificateKeyFile /etc/ssl/application/private.key

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

    ErrorLog /var/log/httpd/application_https_error.log
    CustomLog /var/log/httpd/application_https_access.log combined
    
</VirtualHost>
    

Restart the Apache HTTPD after saving the conf file

Script copied
[root@testing ~]# sudo systemctl restart httpd
Apache Status
LAMP Script with Copy Button

For Debian/Ubuntu-based Systems

Script copied
[root@testing ~]# sudo apt install apache2
Script copied
[root@testing ~]# sudo a2enmod proxy && sudo a2enmod proxy_http && sudo a2enmod proxy_balancer && sudo a2enmod lbmethod_byrequests && sudo systemctl restart apache2
Script copied
[root@testing ~]# sudo nano /etc/apache2/sites-available/reverse-proxy.conf
Script copied
[root@testing ~]# sudo a2ensite reverse-proxy.conf
Script copied
[root@testing ~]# sudo systemctl reload apache2

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page