Introduction

  • Purpose: A reverse proxy sits in front of web servers and forwards client (e.g., browser) requests to those web servers. This setup can increase security, performance, and reliability.
  • Apache as a Reverse Proxy: Apache, a popular web server software, can also function as a reverse proxy. It directs client requests to the appropriate backend server.

Nework Scenario

We have two applications that are running with the Tomcat server at localhost port 8080. No matter whether applications are running with Tomcat or any other service like Nodejs etc. Both local applications’ URLs are:

Advertisement
  • http://localhost:8080/demo1
  • http://localhost:8080/demo2

Now I have installed the Apache server on the same host running on port 80. The Apache server accepts the requests from internet users and forwards them to corresponding applications running on the back end. We need to configure the proxy to forward requests as follows:

  • http://example.com >> http://localhost:8080/demo1/
  • http://example.net >> http://localhost:8080/demo2/
  • http://domain.com/demo1/ >> http://localhost:8080/demo1/
  • http://domain.com/demo2/ >> http://localhost:8080/demo2/

To get a better understanding see the below diagram:

Setup Apache as Reverse Proxy

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.

  • First Virtual Host Example:

    To forward all requests sent to example.com to backend tomcat server corresponding application like:

    • http://example.com >> http://localhost:8080/demo1/

    Configure the first virtual host as below:

  • Second Virtual Host Example:

    To forward all requests sent to example.net to backend tomcat server corresponding application like:

    • http://example.net >> http://localhost:8080/demo2/

    Configure a virtual host like this.

  • Third Virtual Host Example:

    To forward all requests sent to subdirectory /demo1/ or /demo2 on http://domain.com to back-end tomcat corresponding applications like:

    • http://domain.com/demo1/ >> http://localhost:8080/demo1/
    • http://domain.com/demo2/ >> http://localhost:8080/demo2/

    Configure a virtual host like this.

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 
    

Troubleshooting Tips

  • Always check Apache logs for errors: /var/log/apache2/error.log on Ubuntu/Debian or /var/log/httpd/error_log on CentOS/RHEL.
  • Ensure backend servers are correctly configured and accessible from the Apache server.

Conclusion

By following these steps, you’ve set up Apache as a reverse proxy. This setup enhances security and efficiency by managing traffic to your backend servers. Remember, each environment is different, so some adjustments might be necessary for your specific setup.

Share.

4 Comments

  1. ashish bagayatkar on

    Hi Rahul,

    I have separate web and tomcat server. Revers proxy is configured on Web server i.e Apache to tomcat. Now Tomcat has one application which has to hit some URL . Now the request has to from Web server .
    Please suggest where I have to configure reverse proxy which will pass request to web server.

  2. Wessel van Norel on

    I just found this tutorial and I’m amazed that you set ProxyRequests to On.

    https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxyrequests

    “This allows or prevents Apache httpd from functioning as a forward proxy server. (Setting ProxyRequests to Off does not disable use of the ProxyPass directive.)”

    With this setting you allow anyone to use your server as an open proxy server, to be (ab)used by anyone on the internet. Are you sure you intent this?

  3. Hi Rahul,
    Your blog is useful…..but still now facing problem …I am working azure cloud vm and am installed tomcat and Apache 2.4 ….As per above setup all ..But browser when hitting site always open Apache Tomcat index page.When am hitting localhost open my war file cannot open outside access…Can u help regarding problem..Thanx.

Leave A Reply

Exit mobile version