Quantcast
Channel: Apache – TecAdmin
Viewing all articles
Browse latest Browse all 10

How to Configure Apache as a Reverse Proxy

$
0
0

Configuring Apache as a reverse proxy means setting up Apache to listen and direct web traffic to backend servers or services. This can help manage and balance the load on your servers, improve security, and make your web services more efficient. You can also setup this to listen request on standard HTTP and HTTPS ports and redirect 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’re new to this, don’t worry – we’ll make it easy to understand and follow. By the end, you’ll have a working reverse proxy that helps your web applications run smoothly.

Network Scenario

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

  • One application is running on the same server as Apache but uses a different port, like 3000.
  • Other applications are running on a different server within the same network, but this server is not accessible from the internet.
Apache Reverse Proxy Setup
Apache Reverse Proxy Setup

So, let’s the configuration start:

Step 1: Setup Apache Proxy Module

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

  • Redhat-based systems: Edit the proxy configuration file /etc/httpd/conf.modules.d/00-proxy.conf uncomment the following entries. If not available, then 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 Apache services to apply changes immediately.

Step 2: Configure Apache Virtual Host

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

  1. 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>
    
    
  2. Reverse Proxy to Local with Sub URL

    To forward specific subdirectory URL 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>
    
    
  3. Reverse Proxy to 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>
    
    
  4. Reverse Proxy to Multiple Backend Applications

    To forward requests to different backend applications based on the URL path, for example, forwarding requests to www.yourdomain.com/app1 to an application on port 3000, and 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>
    
    
  5. Reverse Proxy to Application on Different Ports

    To forward requests to different backend applications on the same server but different ports, for example, forwarding requests to www.yourdomain.com/app1 to an application on port 3000, and 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 Changes

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

  • Redhat-based systems:
    sudo systemctl restart httpd 
    
  • Debed-based systems:
    sudo systemctl restart apache2 
    

Conclusion

Hope this tutorial helps you to understand using reverse proxy with Apache. Setting up Apache as a reverse proxy can help manage your web applications more effectively. Whether you need to direct traffic to local applications or to the different servers, configuring a reverse proxy can improve your system’s performance and security. By following the examples provided in this tutorial, you can easily set up and customize your reverse proxy to meet your specific needs.

The post How to Configure Apache as a Reverse Proxy appeared first on TecAdmin.


Viewing all articles
Browse latest Browse all 10

Trending Articles