Networking

How to set up a reverse proxy on Ubuntu Server 18.04

Find out how to create a reverse proxy on the latest iteration of the Ubuntu Server platform. This tutorial will walk you through the basics.

apachehero.jpg
Image: Jack Wallen

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)

Dependencies

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

Backend servers

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:

nano ~/backend_server_1.py

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:

curl http://127.0.0.1:8080

You should see Backend 1 in the output (Figure A).

Figure A

Figure A

The Backend 1 server is listening.


Now issue the command:

curl http://127.0.0.1:8081

This time you should see Backend 2 reporting (Figure B).

Figure B

Figure B

Backend 2 reporting in.


Configuring Apache

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.

Also read...

Other tips?

Got a favorite open source trick or time-savers? Share it with fellow TechRepublic members in the Comments section below.

About Jack Wallen

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.

Editor's Picks

Free Newsletters, In your Inbox