Last updated on August 10th, 2020 at 03:05 am
Apache Reverse Proxy allows you to run one or more backend servers behind a proxy server, without exposing them publicly. It also helps with load balancing. Here’s Apache Reverse Proxy Configuration for Ubuntu/Debian Linux.
Apache Reverse Proxy Configuration Step By Step
Here’s a step by step Apache Reverse Proxy Configuration for Ubuntu/Debian Linux systems.
1. Install Required Apache modules
We require 4 Apache modules to configure Apache reverse proxy – mod_proxy, mod_proxy_http, mod_proxy_balancer, mod_lbmethod_byrequests
- mod_proxy is the main proxy module that redirects requests and allows Apache to act as gateway to backend servers
- mod_proxy_http allows support for proxying HTTP requests
- mod_proxy_balancer and mod_lbmethod_byrequests add load balancing capabilities to Apache web server.
Open terminal and run the following commands to install pre-requisite Apache modules.
$ sudo a2enmod proxy $ sudo a2enmod proxy_http $ sudo a2enmod proxy_balancer $ sudo a2enmod lbmethod_byrequests
Bonus Read : How to Install & Configure mod_pagespeed with Apache
2. Restart Apache Server
Restart Apache Server to apply changes
$ sudo service apache2 restart
3. Setup backend server
We will install & setup flask server to run on port 8080 as a backend server.
$ sudo apt-get update $ sudo apt-get -y install python3-pip $ sudo pip3 install flask
Flask comes with a readymade ~/backend.py file that returns “Hello World” on requesting home page.
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello world!'
Bonus Read : How to Check if mod_expires is enabled
We will run flask server
$ FLASK_APP=~/backend.py flask run --port=8080 >/dev/null 2>&1 &
You can test this server by running curl command
$ curl http://127.0.0.1:8080/
You will see the output as Hello World!
4. Update Apache Configuration
We need to modify Apache’s default configuration file to enable reverse proxy. Open Apache configuration in text editor
$ sudo vi /etc/apache2/sites-available/000-default.conf
Add the following lines to VirtualHost tag in Apache configuration file.
ProxyPreserveHost On ProxyPass / http://127.0.0.1:8080/ ProxyPassReverse / http://127.0.0.1:8080/
Let’s look at the 3 directives.
- ProxyPreserveHost causes Apache to preserve original host header and pass it to back-end server.
- ProxyPass is the main proxy directive which states that everything under root (/) should be directed to back-end server. If Apache gets request for /example, then it will send the request to http://your_backend_server/example
- ProxyPassReverse tells Apache to modify response header in the response received from back-end server. This is useful in case the back-end server returns a location redirect response, then the client will be redirected to Apache proxy server, instead of back-end server.
<VirtualHost *:80> ... ProxyPreserveHost On ProxyPass / http://127.0.0.1:8080/ ProxyPassReverse / http://127.0.0.1:8080/ ... </VirtualHost>
Bonus Read : How to Enable mod_headers in Apache
5. Restart Apache Server
Restart Apache Server to apply changes
$ sudo service apache2 restart
Hopefully, the above Apache reverse proxy configuration will help you setup proxy server for your website.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.