How to configure Apache as a reverse proxy

4 giu 2024 4 min di lettura
How to configure Apache as a reverse proxy
Indice dei contenuti

Introduction

Configuring Apache as a reverse proxy means configuring Apache to listen and direct web traffic to servers or backend services. This can help you manage and balance the load on your servers, improve security, and make your web services more efficient. You can also configure it to listen for the request on standard HTTP and HTTPS ports and redirect it to backend services running on different ports.

In this guide we will show you how to set up Apache as a reverse proxy in simple steps. Even if you are new to this field, don't worry - we will make it easy to understand and follow. In the end, you will have a working reverse proxy that will help your web applications run smoothly.

Network scenario

Imagine you have Apache installed on a server that anyone can access from the Internet. Apache listens for traffic on the usual HTTP and HTTPS ports. You also have a few applications:

  • An application is running on the same server as Apache but uses a different port, such as 3000.
  • Other applications are running on a different server within the same network, but this server is not accessible from the Internet.

So, let's start the configuration:

Step 1: Configuring the Apache Proxy Module

By default, this module is enabled in Apache for users who have installed using rpm packages. Debian-based users must enable modules manually.

RedHat based systems: Edit the proxy configuration file /etc/httpd/conf.modules.d/00-proxy.conf and uncomment the following entries. If not available, add them:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Debian-based systems: Use the following command to enable the Proxy module with Apache.

sudo a2enmod proxy proxy_http

After enabling the modules, you will need to restart the Apache services to apply the changes immediately.

Step 2: Set up the Apache virtual host

Now it will start working with the virtual host. We are creating three virtual hosts as below. You create only what is required with the necessary modifications. Edit the main Apache configuration file and get started with the configuration.

Reverse proxy to local application

To forward all requests sent to www.yourdomain.com to a backend application running locally on port 3000:

<VirtualHost *:80>
 ServerName www.yourdomain.com

 ProxyPreserveHost On

 # Reverse proxy for the application running on port 3000 on the same server
 ProxyPass /http://localhost:3000/ProxyPassReverse /http://localhost:3000/# Change log as per server
 # ErrorLog ${APACHE_LOG_DIR}/error.log
 # CustomLog ${APACHE_LOG_DIR}/access.log combined
 </VirtualHost>

Reverse proxy to local with secondary URL

To forward the URL of a specific subdirectory to a backend application. For example, to forward all requests sent to www.yourdomain.com/api to a backend application running locally on port 3000:

<VirtualHost *:80>
 ServerName www.yourdomain.com

 ProxyPreserveHost On

 # Reverse proxy for the application running on port 3000 on the same server
 ProxyPass /api http://localhost:3000/ProxyPassReverse /api http://localhost:3000/# Change log as per server
 # ErrorLog ${APACHE_LOG_DIR}/error.log
 # CustomLog ${APACHE_LOG_DIR}/access.log combined
 </VirtualHost>

Reverse proxy for the backend host application

To forward all requests sent to www.yourdomain.com to a backend application running on a different server (IP 192.168.1.100) on port 3000:

<VirtualHost *:80>
 ServerName www.yourdomain.com

 ProxyPreserveHost On

 # Reverse proxy for the application running on a different server
 ProxyPass /http://192.168.1.100:3000/ProxyPassReverse /http://192.168.1.100:3000/# Change log as per server
 # ErrorLog ${APACHE_LOG_DIR}/error.log
 # CustomLog ${APACHE_LOG_DIR}/access.log combined
 </VirtualHost>

Reverse proxy for multiple backend applications

To forward requests to different backend applications by URL path, for example, forward requests to www.yourdomain.com/app1 to an application on port 3000 and forward requests to www.yourdomain.com/app2 to an application on port 5000 on a different server (IP 192.168.1.100):

<VirtualHost *:80>
 ServerName www.yourdomain.com

 ProxyPreserveHost On

 # Reverse proxy for different applications
 ProxyPass /app1 http://localhost:3000/ProxyPassReverse /app1 http://localhost:3000/ProxyPass /app2 http://192.168.1.100:5000/ProxyPassReverse /app2 http://192.168.1.100:5000/# Change log as per server
 # ErrorLog ${APACHE_LOG_DIR}/error.log
 # CustomLog ${APACHE_LOG_DIR}/access.log combined
 </VirtualHost>

Reverse proxy for applying on different ports

To forward requests to different backend applications on the same server but on different ports, for example, forward requests to www.yourdomain.com/app1 to an application on port 3000 and forward requests to www.yourdomain.com/app2 to an application on port 5000:

<VirtualHost *:80>
 ServerName www.yourdomain.com

 ProxyPreserveHost On

 # Reverse proxy for different applications
 ProxyPass /app1 http://localhost:3000/ProxyPassReverse /app1 http://localhost:3000/ProxyPass /app2 http://localhost:5000/ProxyPassReverse /app2 http://localhost:5000/# Change log as per server
 # ErrorLog ${APACHE_LOG_DIR}/error.log
 # CustomLog ${APACHE_LOG_DIR}/access.log combined
 </VirtualHost>

Step 3: Restart Apache to apply the changes

Once the Apache virtual host is successfully created, you need to restart the Apache service. Use the following commands to restart the Apache service based on your operating system.

Debed-based systems:

sudo systemctl restart httpd

RedHat based systems:

sudo systemctl restart apache2

Conclusion

I hope this tutorial helps you understand the use of reverse proxy with Apache. Setting up Apache as a reverse proxy can help you manage your web applications more effectively. If you need to route traffic to local applications or different servers, setting up a reverse proxy can improve the performance and security of your system. By following the examples provided in this tutorial, you can easily set up and customize your own reverse proxy to meet your specific needs.

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.