Configure Apache reverse proxy with caching for microservices

Intermediate 25 min Jun 11, 2026 314 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Set up Apache HTTP Server as a reverse proxy with intelligent caching for microservices architectures. This tutorial covers mod_proxy, mod_cache configuration, cache policies, and monitoring for high-performance service delivery.

Prerequisites

  • Root or sudo access
  • Basic understanding of HTTP and web services
  • Microservices running on different ports

What this solves

A reverse proxy with caching reduces load on backend microservices by serving cached responses for repeated requests. Apache's mod_proxy and mod_cache modules provide flexible routing, load balancing, and intelligent caching that can dramatically improve response times and reduce resource usage across your service architecture.

Step-by-step configuration

Update system packages

Start by updating your package manager to ensure you have the latest security patches and package versions.

sudo apt update && sudo apt upgrade -y
sudo dnf update -y

Install Apache HTTP Server

Install Apache web server which includes the proxy and caching modules we need.

sudo apt install -y apache2 apache2-utils
sudo dnf install -y httpd httpd-tools

Enable required Apache modules

Enable the proxy, cache, and related modules needed for reverse proxy functionality with caching.

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
sudo a2enmod cache
sudo a2enmod cache_disk
sudo a2enmod headers
sudo a2enmod rewrite
# Modules are loaded via LoadModule directives in configuration
</code><h2><code>We'll configure these in the next steps</code></h2></pre>
</div>
</div>
</div>

<div class="step">
### Create cache directory
<p>Create a dedicated directory for Apache to store cached content with proper permissions.</p>
<pre class="terminal"><code>sudo mkdir -p /var/cache/apache2/mod_cache_disk
sudo chown www-data:www-data /var/cache/apache2/mod_cache_disk
sudo chmod 755 /var/cache/apache2/mod_cache_disk
Never use chmod 777. It gives every user on the system full access to your files. The web server only needs write access to the cache directory, which we provide with proper ownership.

Configure main Apache settings

Configure the main Apache configuration with performance optimizations for proxy and caching workloads.

# Add these settings at the end of the file

# Performance tuning for proxy workloads
MaxRequestWorkers 400
ThreadsPerChild 25
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250

# Proxy timeout settings
ProxyTimeout 300
ProxyPreserveHost On

# Security headers
Header always set X-Frame-Options DENY
Header always set X-Content-Type-Options nosniff
Header always set Referrer-Policy strict-origin-when-cross-origin
# Add these settings at the end of the file

# Load required modules
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule cache_module modules/mod_cache.so
LoadModule cache_disk_module modules/mod_cache_disk.so
LoadModule headers_module modules/mod_headers.so
LoadModule rewrite_module modules/mod_rewrite.so

# Performance tuning for proxy workloads
MaxRequestWorkers 400
ThreadsPerChild 25
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250

# Proxy timeout settings
ProxyTimeout 300
ProxyPreserveHost On

# Security headers
Header always set X-Frame-Options DENY
Header always set X-Content-Type-Options nosniff
Header always set Referrer-Policy strict-origin-when-cross-origin

Create virtual host configuration

Create a virtual host that configures reverse proxy with caching for your microservices.

Create cache directory for Red Hat systems

On Red Hat-based systems, create the cache directory with SELinux context.

# Cache directory already created in previous step
sudo mkdir -p /var/cache/httpd/mod_cache_disk
sudo chown apache:apache /var/cache/httpd/mod_cache_disk
sudo chmod 755 /var/cache/httpd/mod_cache_disk
sudo setsebool -P httpd_can_network_connect 1
sudo semanage fcontext -a -t httpd_cache_t "/var/cache/httpd/mod_cache_disk(/.*)?" || true
sudo restorecon -R /var/cache/httpd/mod_cache_disk

Enable the virtual host

Enable the new virtual host configuration and disable the default site.

sudo a2ensite microservices-proxy.conf
sudo a2dissite 000-default.conf
# Configuration is automatically loaded from conf.d directory

Configure cache cleanup

Set up automatic cache cleanup to prevent disk space issues. Create a systemd timer to clean old cache files.

[Unit]
Description=Apache Cache Cleanup
After=network.target

[Service]
Type=oneshot
User=www-data
Group=www-data
ExecStart=/usr/bin/find /var/cache/apache2/mod_cache_disk -type f -mtime +7 -delete
ExecStart=/usr/bin/find /var/cache/apache2/mod_cache_disk -type d -empty -delete
[Unit]
Description=Run Apache Cache Cleanup Daily
Requires=apache-cache-cleanup.service

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now apache-cache-cleanup.timer

Test configuration and start Apache

Test the Apache configuration for syntax errors and start the service.

sudo apache2ctl configtest
sudo systemctl enable --now apache2
sudo systemctl status apache2
sudo httpd -t
sudo systemctl enable --now httpd
sudo systemctl status httpd

Configure firewall rules

Open HTTP and HTTPS ports in the firewall for web traffic.

sudo ufw allow 'Apache Full'
sudo ufw reload
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Configure cache policies and optimization

Advanced cache configuration

Fine-tune cache behavior with advanced policies for different types of content and API responses.

# Cache size limits and memory settings
CacheMaxFileSize 10000000
CacheMinFileSize 1
CacheReadSize 102400
CacheReadTime 3600

# Ignore certain headers that prevent caching
CacheIgnoreHeaders Set-Cookie
CacheIgnoreQueryString Off
CacheKeyBaseURL http://api.example.com

# Cache based on Accept-Encoding for compressed content
CacheStorePrivate On
CacheStoreNoStore Off

# Advanced cache policies






# Don't cache authentication endpoints
# Cache size limits and memory settings
CacheMaxFileSize 10000000
CacheMinFileSize 1
CacheReadSize 102400
CacheReadTime 3600

# Ignore certain headers that prevent caching
CacheIgnoreHeaders Set-Cookie
CacheIgnoreQueryString Off
CacheKeyBaseURL http://api.example.com

# Cache based on Accept-Encoding for compressed content
CacheStorePrivate On
CacheStoreNoStore Off

# Advanced cache policies






# Don't cache authentication endpoints

Enable cache optimization

Enable the cache optimization configuration and reload Apache.

sudo a2enconf cache-optimization
sudo apache2ctl configtest
sudo systemctl reload apache2
sudo httpd -t
sudo systemctl reload httpd

Monitor and troubleshoot reverse proxy

Set up cache monitoring script

Create a monitoring script to track cache performance and hit rates.

#!/bin/bash

# Apache cache monitoring script
CACHE_DIR="/var/cache/apache2/mod_cache_disk"
LOG_FILE="/var/log/apache2/microservices_cache.log"

# Cache disk usage
echo "Cache Directory Usage:"
du -sh $CACHE_DIR
echo ""

# Cache files count
echo "Cache Files Count:"
find $CACHE_DIR -type f | wc -l
echo ""

# Recent cache statistics from logs
echo "Cache Hit/Miss Statistics (last 1000 requests):"
if [ -f "$LOG_FILE" ]; then
    tail -1000 $LOG_FILE | awk '{
        if ($NF ~ /HIT/) hits++;
        else if ($NF ~ /MISS/) misses++;
        total++
    } END {
        if (total > 0) {
            hit_rate = (hits/total)*100;
            printf "Hits: %d (%.1f%%)\n", hits, hit_rate;
            printf "Misses: %d (%.1f%%)\n", misses, 100-hit_rate;
            printf "Total: %d\n", total;
        } else {
            print "No cache data found in logs";
        }
    }'
else
    echo "Cache log file not found"
fi

# Top requested URLs being cached
echo ""
echo "Top 10 Cached URLs:"
if [ -f "$LOG_FILE" ]; then
    tail -1000 $LOG_FILE | awk '{print $7}' | sort | uniq -c | sort -nr | head -10
fi
sudo chmod +x /usr/local/bin/apache-cache-stats.sh

Configure l

Automated install script

Run this to automate the entire setup

Sie möchten das nicht selbst verwalten?

Wir betreiben Infrastruktur für Unternehmen, die auf Verfügbarkeit angewiesen sind. Vollständig verwaltet, mit einem festen Ansprechpartner, der Ihre Umgebung kennt.

Sie erhalten einen festen Ansprechpartner, der Ihr Setup kennt

Rotterdam 19:03 · erreichbar per Nachricht, kein Ticketformular