
Normally these days we use Nginx as reverse proxy. Like, in an old guide we have shown how to use Nginx reverse proxy to add an IPv6 address to IPv4 only dedicated server. Also we provided configuration of such for HSTS website. We actually can use Apache web server as reverse proxy. Here is How to Configure Apache as Reverse Proxy on Single Cloud Server Instance Running Ubuntu 16.04 For IPv4. We will use some cloud server like VPSDime $7 per month 6 GB OpenVZ instance for this work. For very small need, we can use 1GB RAM at 1 Euro Per Month Aruba Cloud VMWare instance. We will set up Apache as a basic reverse proxy using the mod_proxy extension to redirect the incoming connections to one or several backend servers.

Required Modules to Configure Apache as Reverse Proxy

The modules that are needed to use Apache as a reverse proxy includes :

mod_proxy : the core proxy module Apache module for redirecting connections mod_proxy_http : adds support for proxying HTTP and HTTPS connections. mod_proxy_balancer : adds load balancing features. mod_lbmethod_byrequests : adds load balancing features. mod_proxy_ftp : This handles fetching documents with FTP. mod_proxy_connect : This handles the CONNECT method for secure tunnelling. mod_proxy_ajp : This handles the AJP protocol for Tomcat like backend servers. mod_proxy_html: This rewrites HTML links into a proxy’s address space. mod_xml2enc : This supports internationalisation behalf of mod_proxy_html. mod_headers: This modifies HTTP request and response headers. mod_deflate: Negotiates compression with clients and backends. mod_cache : To enable caching. mod_disk_cache : To enable caching. mod_mem_cache : To enable caching.

How to Configure Apache as Reverse Proxy

First update and upgrade :

Advertisement ---

apt update -y 1 apt update - y

apt upgrade -y 1 apt upgrade - y

Now, we will install Apache on our server :

apt install apache2 -y 1 apt install apache2 - y

Once Apache has been installed, we need to start the Apache service, configure it to start automatically restart on reboot. Open :

sudo /etc/init.d/apache2 start sudo update-rc.d apache2 defaults 1 2 sudo / etc / init . d / apache2 start sudo update - rc . d apache2 defaults

We have to install mod_proxy and dependencies :

apt install libapache2-mod-proxy-html libxml2-dev build-essential -y 1 apt install libapache2 - mod - proxy - html libxml2 - dev build - essential - y

Run the below command to get the list of available modules:

sudo a2enmod 1 sudo a2enmod

We need to run commands to enable the modules we need, one by one :

sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_ajp sudo a2enmod rewrite sudo a2enmod deflate sudo a2enmod headers sudo a2enmod proxy_balancer sudo a2enmod proxy_connect sudo a2enmod proxy_html 1 2 3 4 5 6 7 8 9 sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_ajp sudo a2enmod rewrite sudo a2enmod deflate sudo a2enmod headers sudo a2enmod proxy_balancer sudo a2enmod proxy_connect sudo a2enmod proxy_html

Apache default configuration file is usually 000-default.conf , we can create our own virtual host file at /etc/apache2/sites-available location :

sudo a2dissite 000-default touch /etc/apache2/sites-available/proxy-http nano /etc/apache2/sites-available/proxy-http 1 2 3 sudo a2dissite 000 - default touch / etc / apache2 / sites - available / proxy - http nano / etc / apache2 / sites - available / proxy - http

Now, add something like this on that /etc/apache2/sites-available/proxy-http file :

/etc/apache2/sites-available/proxy-http ServerAdmin admin@example.com DocumentRoot /var/www/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined ProxyPreserveHost On ProxyPass / http://server-ip-address:8080/ ProxyPassReverse / http://server-ip-address:8080/ ServerName example.com 1 2 3 4 5 6 7 8 ServerAdmin admin @example . com DocumentRoot / var / www / ErrorLog $ { APACHE_LOG_DIR } / error . log CustomLog $ { APACHE_LOG_DIR } / access . log combined ProxyPreserveHost On ProxyPass / http : / / server - ip - address : 8080 / ProxyPassReverse / http : / / server - ip - address : 8080 / ServerName example . com

Save and close the file. Enable new virtual host file:

sudo a2ensite proxy-http 1 sudo a2ensite proxy - http

We may also need to direct Apache to listen on port 8080. “May”, as it depends. That can be done by editing the ports.conf file:

nano /etc/apache2/ports.conf 1 nano / etc / apache2 / ports . conf

Directive will be :

Listen 8080 1 Listen 8080

If you change, you need to restart :

sudo /etc/init.d/apache2 restart 1 sudo / etc / init . d / apache2 restart

In the above change of port case, http://i.p.a.d.d.r.e.ss:80 will show the application which is running on http://i.p.a.d.d.r.e.ss:8080. If we want to enable SSL support, then we will need to enable the SSL module first.

sudo a2enmod ssl sudo /etc/init.d/apache2 restart 1 2 sudo a2enmod ssl sudo / etc / init . d / apache2 restart

Next, you will need to use Let’s Encrypt certificate in the way we have shown for Nginx in different guide.

We can copy all the certificate files to the /etc/apache2/ssl directory. We will create new virtual host file named proxy-https :

nano /etc/apache2/sites-available/proxy-https 1 nano / etc / apache2 / sites - available / proxy - https

Here is example config :

ServerAdmin admin@example.com DocumentRoot /var/www/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine On SSLCertificateFile /etc/apache2/ssl/ca.crt SSLCertificateKeyFile /etc/apache2/ssl/ca.key ProxyPreserveHost On ProxyPass /var/www/ http://server-ip-address:8080/ ProxyPassReverse /var/www/ http://server-ip-address:8080/ ServerName example.com 1 2 3 4 5 6 7 8 9 10 11 ServerAdmin admin @example . com DocumentRoot / var / www / ErrorLog $ { APACHE_LOG_DIR } / error . log CustomLog $ { APACHE_LOG_DIR } / access . log combined SSLEngine On SSLCertificateFile / etc / apache2 / ssl / ca . crt SSLCertificateKeyFile / etc / apache2 / ssl / ca . key ProxyPreserveHost On ProxyPass / var / www / http : / / server - ip - address : 8080 / ProxyPassReverse / var / www / http : / / server - ip - address : 8080 / ServerName example . com

Save and close the file. Enable new virtual host file:

sudo a2ensite proxy-https sudo /etc/init.d/apache2 restart 1 2 sudo a2ensite proxy - https sudo / etc / init . d / apache2 restart

There is huge documentation on Apache’s site :

https://httpd.apache.org/docs/2.4/mod/mod_proxy.html 1 https : / / httpd . apache . org / docs / 2.4 / mod / mod_proxy . html

We provided quite basic examples on how to configure Apache as reverse proxy, but possibly that is the critical part.

Tagged With

This Article Has Been Shared 359 Times! Pinterest

About Abhishek Ghosh Abhishek Ghosh is a Businessman, Orthopaedic Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.