This will come from the workplace of “You may well not at any time will need it, but when you do, you are going to be glad you know how.”
A reverse proxy is a proxy server that accepts HTTP (or HTTPS) requests and mechanically feeds them to backend servers. This can be valuable when you have a website that capabilities with backend programs that will need to be fed requests instantly from the website. With the aid of Apache, you can make this transpire with a relative amount of money of ease. I’m going to wander by means of the method of environment up a fundamental reverse proxy on the most recent model of Ubuntu Server (eighteen.04).
What you are going to will need
You can expect to will need a server (or virtual equipment) functioning Ubuntu Server eighteen.04 with Apache put in. If your server doesn’t already have Apache put in, you can achieve that with the one command sudo apt install lamp-server^.
With that out of the way, let’s make this transpire.
SEE: Securing Linux plan (Tech Professional Research)
We are going to be operating with a instrument termed Flask, a micro-framework prepared in Python made use of for the progress of world wide web programs. Open up a terminal window and install Flask with the subsequent commands:
sudo apt-get update sudo apt-get -y install python3-pip sudo pip3 install flask
Upcoming we will need to help a couple Apache modules. To do this, difficulty the subsequent commands:
sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_balancer sudo a2enmod lbmethod_byrequests
Apache will now will need to be restarted with the command:
sudo systemctl restart apache2
Now we will need to create two very simple backend servers. We are going to create these with a bare least of configuration and selections. Produce the configuration file for the to start with backend server with the command:
In that file, duplicate the subsequent material:
from flask import Flask app = Flask(__identify__) @app.route('/') def house(): return 'Backend one!'
Save and close that file. Produce a different configuration file termed backend_server_two.py with the contents:
from flask import Flask app = Flask(__identify__) @app.route('/') def house(): return 'Backend two!'
Save and close that file.
Now we use the Flask instrument to get started the to start with qualifications server on port 8080 and redirect the output to /dev/null. The command for this is:
FLASK_Application=~/backend_server_one.py flask operate --port=8080 >/dev/null 2>&one &
Start off the 2nd backend server (on port 8081) with the command:
FLASK_Application=~/backend_server_two.py flask operate --port=8081 >/dev/null 2>&one &
SEE: twenty swift suggestions to make Linux networking less difficult (free of charge TechRepublic PDF)
Screening the setup
Let’s examination the method so significantly. From the terminal window, difficulty the command:
You really should see Backend one in the output (Determine A).
Now difficulty the command:
This time you really should see Backend two reporting (Determine B).
At this place, you can attain those people backend servers only from the localhost handle (127…one). If you will need to access the reverse proxy from your LAN, you will need to reconfigure Apache. In advance of you do this, make a duplicate of your recent Apache configuration file with the command:
sudo cp /and many others/apache2/web sites-out there/000-default.conf /and many others/apache2/web sites-out there/000-default.conf.bak
Problem the command:
sudo nano /and many others/apache2/web sites-out there/000-default.conf
Switch the complete contents of that file with the subsequent:
ProxyPreserveHost On ProxyPass / http://127...one:8080/ ProxyPassReverse / http://127...one:8080/
Save and close that file. Restart Apache with the command:
sudo systemctl restart apache2
If you however have the unique Apache index.html file in /var/www/html/ go it with the command:
sudo mv /var/www/html/index.html /var/www/html/index.html.bak
Now place a world wide web browser to http://SERVER_IP (wherever SERVER_IP is the IP handle of your server). You really should now see Backend one printed in the browser. The reverse proxy is operating.
That’s all there is to it
Females and gents, you now have a fundamental reverse proxy up and functioning on Ubuntu Server eighteen.04. Relying upon your requires, this really should provide as a starting place to get you wherever you will need to go. This will arrive in helpful if you have to have passing connections from a world wide web server to a backend application server.
Got a favorite open resource trick or time-savers? Share it with fellow TechRepublic customers in the Responses portion under.