This comes from the office of "You might not ever need it, but when you do, you'll be glad you know how."
A reverse proxy is a proxy server that accepts HTTP (or HTTPS) requests and automatically feeds them to backend servers. This can be helpful when you have a website that functions with backend applications that need to be fed requests directly from the website. With the help of Apache, you can make this happen with a relative amount of ease. I'm going to walk through the process of setting up a basic reverse proxy on the latest version of Ubuntu Server (18.04).
What you'll need
You'll need a server (or virtual machine) running Ubuntu Server 18.04 with Apache installed. If your server doesn't already have Apache installed, you can accomplish that with the single command sudo apt install lamp-server^.
With that out of the way, let's make this happen.
SEE: Securing Linux policy (Tech Pro Research)
We are going to be working with a tool called Flask, a micro-framework written in Python used for the development of web applications. Open a terminal window and install Flask with the following commands:
sudo apt-get update sudo apt-get -y install python3-pip sudo pip3 install flask
Next we need to enable a few Apache modules. To do this, issue the following commands:
sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_balancer sudo a2enmod lbmethod_byrequests
Apache will now need to be restarted with the command:
sudo systemctl restart apache2
Now we need to create two simple backend servers. We're going to create these with a bare minimum of configuration and options. Create the configuration file for the first backend server with the command:
In that file, copy the following content:
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Backend 1!'
Save and close that file. Create another configuration file called backend_server_2.py with the contents:
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Backend 2!'
Save and close that file.
Now we use the Flask tool to start the first background server on port 8080 and redirect the output to /dev/null. The command for this is:
FLASK_APP=~/backend_server_1.py flask run --port=8080 >/dev/null 2>&1 &
Start the second backend server (on port 8081) with the command:
FLASK_APP=~/backend_server_2.py flask run --port=8081 >/dev/null 2>&1 &
SEE: 20 quick tips to make Linux networking easier (free TechRepublic PDF)
Testing the setup
Let's test the process so far. From the terminal window, issue the command:
You should see Backend 1 in the output (Figure A).
Now issue the command:
This time you should see Backend 2 reporting (Figure B).
At this point, you can reach those backend servers only from the localhost address (127.0.0.1). If you need to access the reverse proxy from your LAN, you need to reconfigure Apache. Before you do this, make a copy of your current Apache configuration file with the command:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default.conf.bak
Issue the command:
sudo nano /etc/apache2/sites-available/000-default.conf
Replace the entire contents of that file with the following:
<VirtualHost *:80> ProxyPreserveHost On ProxyPass / http://127.0.0.1:8080/ ProxyPassReverse / http://127.0.0.1:8080/ </VirtualHost>
Save and close that file. Restart Apache with the command:
sudo systemctl restart apache2
If you still have the original Apache index.html file in /var/www/html/ move it with the command:
sudo mv /var/www/html/index.html /var/www/html/index.html.bak
Now point a web browser to http://SERVER_IP (where SERVER_IP is the IP address of your server). You should now see Backend 1 printed in the browser. The reverse proxy is working.
That's all there is to it
Ladies and gents, you now have a basic reverse proxy up and running on Ubuntu Server 18.04. Depending upon your needs, this should serve as a starting point to get you where you need to go. This will come in handy if you require passing connections from a web server to a backend application server.
- How to configure, monitor, and manage Apache with ApacheGUI (TechRepublic)
- How to improve Apache server security by limiting the information it reveals (TechRepublic)
- Apache or NGINX: Which web server is right for you? (TechRepublic Video)
- How to speed up Apache with Varnish HTTP cache (TechRepublic)
- We interrupt this revolution: Apache Spark changes the rules of the game (ZDNet)
Got a favorite open source trick or time-savers? Share it with fellow TechRepublic members in the Comments section below.
Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.