close up of computer keyboard

Configuring Reverse Proxy on Apache Server

Share this post on:

A reverse proxy is a powerful tool for managing web traffic, balancing loads, and improving security. In this post, we’ll explore how to configure a reverse proxy using Apache Server, one of the most popular web servers globally.What is a Reverse Proxy?

A reverse proxy sits between clients and backend servers, forwarding client requests to appropriate backend servers and returning the responses to the clients. Unlike a forward proxy that handles outbound traffic, a reverse proxy manages inbound traffic and acts as an intermediary for the backend servers.

Key Benefits of Reverse Proxy:

  • Load Balancing: Distribute incoming requests across multiple backend servers
  • Security: Hide backend server details from the public internet
  • Caching: Store and serve frequently accessed content
  • SSL/TLS Termination: Handle encryption/decryption at the proxy level
  • Compression: Reduce bandwidth usage with content compression
  • Access Control: Implement authentication and authorization

Prerequisites:

Before configuring a reverse proxy on Apache, ensure you have:

  • Apache 2.4 or higher installed
  • mod_proxy and mod_proxy_http modules enabled
  • Root or sudo access to your Apache server
  • Basic understanding of Apache configuration files

Enabling Required Modules:

First, enable the necessary Apache modules:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo systemctl restart apache2

Basic Reverse Proxy Configuration:

Create or edit a virtual host configuration file. Navigate to your Apache sites directory and create a new configuration:

sudo nano /etc/apache2/sites-available/reverse-proxy.conf

Add the following configuration:


ServerName example.com
ServerAdmin admin@example.com

ProxyPreserveHost On
ProxyPass / http://backend-server.local:8080/
ProxyPassReverse / http://backend-server.local:8080/

Configuration Explanation:

  • VirtualHost *:80: Listens on port 80 (HTTP)
  • ServerName: Your public domain
  • ProxyPreserveHost On: Preserves the original host header
  • ProxyPass: Forwards requests to backend server
  • ProxyPassReverse: Modifies response headers to match the proxy URL

Advanced Configuration:

For more control, you can specify path-based routing:


ServerName example.com

ProxyPreserveHost On

ProxyPass /api/ http://api-server.local:3000/
ProxyPassReverse /api/ http://api-server.local:3000/

ProxyPass /static/ http://static-server.local:9000/
ProxyPassReverse /static/ http://static-server.local:9000/

ProxyPass / http://default-server.local:8080/
ProxyPassReverse / http://default-server.local:8080/

Load Balancing Configuration:

For load balancing across multiple backend servers:


ServerName example.com

ProxyPreserveHost On

<Proxy balancer://mycluster>
    BalancerMember http://backend1.local:8080
    BalancerMember http://backend2.local:8080
    BalancerMember http://backend3.local:8080
</Proxy>

ProxyPass / balancer://mycluster/
ProxyPassReverse / balancer://mycluster/

Enabling SSL/TLS:

For production environments, always use HTTPS:

sudo a2enmod ssl
sudo a2enmod rewrite

Then update your configuration:


ServerName example.com

SSLEngine On
SSLCertificateFile /etc/ssl/certs/your-cert.crt
SSLCertificateKeyFile /etc/ssl/private/your-key.key

ProxyPreserveHost On
ProxyPass / http://backend-server.local:8080/
ProxyPassReverse / http://backend-server.local:8080/

Redirect HTTP to HTTPS

ServerName example.com RewriteEngine On RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Testing Your Configuration:

Before applying changes, test the Apache configuration:

sudo apache2ctl -t

If you see “Syntax OK”, enable the site:

sudo a2ensite reverse-proxy.conf
sudo systemctl restart apache2

Verifying the Setup:

Check if your reverse proxy is working:

curl -i http://example.com/

View Apache logs for debugging:

sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/error.log

Troubleshooting Tips:

  1. Module Errors: Ensure all required modules are enabled
  2. Connection Refused: Verify backend server is running and accessible
  3. Header Issues: Use ProxyPreserveHost to maintain original headers
  4. Timeout Issues: Adjust timeout settings if needed
  5. SSL Issues: Check certificate validity and paths

So, configuring a reverse proxy on Apache is a straightforward process that significantly enhances your web infrastructure. By following this guide, you can implement load balancing, improve security, and optimize your web service delivery. For complex setups, consider exploring Nginx or HAProxy as alternatives, but Apache remains a robust choice for reverse proxy configurations.


Discover more from Rupinder Singh

Subscribe to get the latest posts sent to your email.

Author: Rupinder Singh

I am a tireless intelligence seeker, coincidentally I am a computer guy too, who is passionate about Information Tools and Open-Source software. I Read Books, play Computer Games, Climb Mountains, when I am not changing the code.

View all posts by Rupinder Singh >

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Rupinder Singh

Subscribe now to keep reading and get access to the full archive.

Continue reading