Apache Reverse Proxy Configuration on Ubuntu (Step-by-Step Guide)


apache reverse proxy

A reverse proxy receives an HTTP(S) requests and distributes them among multiple web servers. They not only make your website more secure but also help load balancing. Let us look at Apache reverse proxy configuration on Ubuntu.

 

Apache Reverse Proxy Configuration on Ubuntu

Here’s the Apache reverse proxy configuration on Ubuntu.

 

Before we proceed, please ensure you have installed Apache web server on Ubuntu.

 

1. Enable Apache module

Although Apache has many modules to help you extend its functionality, they are disabled by default.

For Apache reverse proxy configuration, we need mod_proxy, mod_proxy_http, mod_proxy_balancer, and mod_lbmethod_byrequests.

  • mod_proxy – proxy module to redirect connections
  • mod_proxy_http – proxy module for http connections
  • mod_lbmethod_byrequests – load balancing module for Apache

Enable these modules with the commands


sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests

 

Restart Apache to apply changes


sudo systemctl restart apache2

 

That’s it! Apache can now act as reverse proxy server. Now let us configure the proxy server

 

2. Update Apache Configuration to Enable Reverse Proxy

We will update the default virtual host in Apache to work as a reverse proxy. If you want to set up another virtual host for this purpose, here’s how you can create virtual hosts in Apache.

Open the default virtual host file,


$ sudo nano /etc/apache2/sites-available/000-default.conf

 

On the first line, you’ll see <VirtualHost *:80>. We look at 2 examples to configure Apache reverse proxy

 

1. Reverse Proxy a Single Backend Server

Replace all code in VirtualHost block with the following


<VirtualHost *:80>
 ProxyPreserveHost On

 ProxyPass / http://127.0.0.1:8080/
 ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>

In the above lines,

  • ProxyPreserveHost – Passes the request header to the backend server.
  • ProxyPass – Proxy redirect directive. It maps all requests sent to this server to the backend server. So a request sent to http://example.com is sent to http://your_backend_server. So if you send example.com/product, it will be mapped to your_backend_server/product. It also returns the response from the backend server to the client.
  • ProxyPassReverse – Tells Apache to modify response headers. So in case of location redirect header, Apache will redirect the client to the proxy, and not the backend server.

 

Restart Apache to apply changes


$ sudo systemctl restart apache2

 

Now if you open a web browser and go to http://your_server, you’ll see the response from your back end server.

 

2. Load Balancing across multiple server

If you have a cluster of multiple back end servers, then here’s how you can distribute your traffic across them

Open the virtual host file as shown above and replace the Virtual Host block with the code below


<VirtualHost *:80>
 <Proxy balancer://cluster>
  BalancerMember http://127.0.0.1:8080
  BalancerMember http://127.0.0.1:8081
 </Proxy>

 ProxyPreserveHost On

 ProxyPass / balancer://cluster/
 ProxyPassReverse / balancer://cluster/
</VirtualHost>

The above configuration is similar to that for single back end server, except for the Proxy block. We name it balancer://cluster. You can name it (cluster) whatever you want. In it we list all our backend servers & ports. In our example, we have used 127.0.0.1:8080 and 127.0.0.1:8081. You can use the IP addresses and port numbers of your servers, as per your requirement.

In this case, the ProxyPass and ProxyReversePass refer to the balancer by its name, instead of its IP address, as we did in case of single back end server.

Restart Apache to apply changes


$ sudo systemctl restart apache2

 

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!