Horje
How To Use Apache as a Reverse-Proxy with mod_proxy on Ubuntu 20.04?

Reverse proxy is a server between client and backend servers, it forwards the client requests to the available backend server. Apache is a popular web server proxy using the mod_proxy module. In this article, we will understand how to use Apache as a reverse proxy with mod_proxy.

Prerequisites

  • A server running Ubuntu 20.04.
  • Apache is installed on your server.
  • Basic knowledge of using the terminal.
  • A user with sudo privileges.

Step 1: Updating the System

  • Check that your system is fully updated with the following commands:
sudo apt update
sudo apt upgrade -y

Step 2: Installing Apache

  • If Apache is not already installed, install it using the following command:
sudo apt install apache2 -y
  • Verify that Apache is running:
sudo systemctl status apache2

Step 3: Enable Necessary Apache Modules

  • Apache needs several modules to act as a reverse proxy. Enable those modules using the following commands:
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
  • After enabling the modules, restart Apache to apply the changes:
sudo systemctl restart apache2

Step 4: Configuring Apache as a Reverse Proxy

  • lets create a new configuration file for your reverse proxy setting let say reverse-proxy.conf:
sudo nano /etc/apache2/sites-available/reverse-proxy.conf
  • then, add the following configuration to the file, replace the backend server address with your actual one:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your_domain.com

ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://backend_server/
ProxyPassReverse / http://backend_server/

<Proxy *>
Order allow,deny
Allow from all
</Proxy>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 5: Enabling the New Configuration

  • Now enable the new configuration file and disable the site configuration and restart the apache.
sudo a2ensite reverse-proxy.conf
sudo a2dissite 000-default.conf
sudo systemctl restart apache2

Step 6: Adjust Firewall Settings

  • If you have enable firewall make sure it allows HTTP and HTTPS traffic:
sudo ufw allow 'Apache Full'

Step 7: Test the Configuration

  • Open your web browser and navigate to your domain (e.g., http://your_domain.com). You should be redirected to the backend server specified in your reverse-proxy.conf file.

Additional Configuration Options

SSL Termination

  • For secure communication, you can enable SSL termination on your reverse proxy. First, enable the SSL module:
sudo a2enmod ssl
  • Next, create an SSL certificate or use a service like Let’s Encrypt to obtain one. Modify your reverse-proxy.conf file to include SSL configuration:
<VirtualHost *:443>
ServerAdmin webmaster@localhost
ServerName your_domain.com

ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://backend_server/
ProxyPassReverse / http://backend_server/

<Proxy *>
Order allow,deny
Allow from all
</Proxy>

SSLEngine on
SSLCertificateFile /etc/ssl/certs/your_domain.com.crt
SSLCertificateKeyFile /etc/ssl/private/your_domain.com.key

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  • Enable the SSL site and restart Apache:
sudo a2ensite default-ssl
sudo systemctl restart apache2

Load Balancing

  • You can also configure Apache to balance the load across multiple backend servers. Modify your configuration as follows:
<Proxy balancer://mycluster>
BalancerMember http://backend_server1
BalancerMember http://backend_server2
BalancerMember http://backend_server3
</Proxy>

<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your_domain.com

ProxyPreserveHost On
ProxyPass / balancer://mycluster/
ProxyPassReverse / balancer://mycluster/

<Proxy *>
Order allow,deny
Allow from all
</Proxy>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  • This configuration distributes requests among the specified backend servers, improving scalability and availability.

Conclusion

Setting up a reverse proxy to forward client requests to backend servers, enhances security with SSL termination and balance load accross multiple servers. this is best for optimizing performance and managing traffic efficintly in production environemnt. by following the step in this article you can easily configure and use apache as a reverse-proxy with mod_proxy on ubuntu.




Reffered: https://www.geeksforgeeks.org


Web Technologies

Related
How to Use Font Awesome On Your WordPress Website? How to Use Font Awesome On Your WordPress Website?
Frontend Development Machine Coding Interview Questions (2024) Frontend Development Machine Coding Interview Questions (2024)
Difference Between AJAX And Fetch API Difference Between AJAX And Fetch API
How to Install an SSL Certificate on Apache that run Ubuntu? How to Install an SSL Certificate on Apache that run Ubuntu?
How to Check if OpenSSL and mod_ssl are Installed on Apache2? How to Check if OpenSSL and mod_ssl are Installed on Apache2?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15