How to Configure Apache Reverse Proxy for Seamless Subdomain Routing on Port 80


13 views

When implementing a multi-server infrastructure, we often need to route specific subdomains to different backend servers while maintaining port 80 accessibility. The key requirement here is to avoid port-based redirection (like :81) while preventing redirect loops.

Ensure these modules are enabled in Server A (192.168.0.5):

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
sudo a2enmod rewrite
sudo a2enmod ssl
sudo systemctl restart apache2

Here's the proper virtual host setup for Server A to proxy requests for owncloud.mydomain.com to Server B:

<VirtualHost *:80>
    ServerName owncloud.mydomain.com
    ServerAdmin admin@mydomain.com
    
    ProxyPreserveHost On
    ProxyPass / http://192.168.0.10/
    ProxyPassReverse / http://192.168.0.10/
    
    ErrorLog ${APACHE_LOG_DIR}/owncloud_error.log
    CustomLog ${APACHE_LOG_DIR}/owncloud_access.log combined
</VirtualHost>

Make sure your DNS records point both the main domain and subdomain to Server A's public IP:

mydomain.com.      IN  A      203.0.113.45
owncloud.mydomain.com. IN  A  203.0.113.45
www.mydomain.com.  IN  CNAME  mydomain.com.

On Server B (192.168.0.10), ensure Apache is configured to accept requests from Server A:

<VirtualHost *:80>
    ServerName owncloud.mydomain.com
    DocumentRoot /var/www/owncloud
    
    <Directory /var/www/owncloud>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

If you encounter redirect loops, add these directives to Server A's configuration:

ProxyRequests Off
<Proxy *>
    Require all granted
</Proxy>

For HTTPS implementation, modify the virtual host with these additions:

<VirtualHost *:443>
    ServerName owncloud.mydomain.com
    SSLEngine on
    SSLCertificateFile /path/to/cert.pem
    SSLCertificateKeyFile /path/to/privkey.pem
    
    ProxyPreserveHost On
    ProxyPass / http://192.168.0.10/
    ProxyPassReverse / http://192.168.0.10/
</VirtualHost>

Add these directives to improve proxy performance:

ProxyRequests Off
ProxyTimeout 300
ProxyBadHeader Ignore
ProxyIOBufferSize 8192

After configuration, test with these commands:

sudo apachectl configtest
sudo systemctl restart apache2

Verify the routing works by accessing http://owncloud.mydomain.com from an external network while monitoring both servers' access logs.


When working with multiple Apache servers in an internal network, you might want to route specific subdomains to different backend servers while maintaining port 80 accessibility. Here's how I solved this exact configuration challenge.

In my setup:

  • Server A (192.168.0.5): Primary server handling DNS, mail (webmail.mydomain.com), and main website (www.mydomain.com)
  • Server B (192.168.0.10): Dedicated server running ownCloud

Initially, attempts to proxy requests to Server B resulted in redirect loops because:

  1. The proxy configuration wasn't properly isolating the subdomain traffic
  2. Server A kept intercepting the ownCloud requests meant for Server B
  3. Missing ProxyPreserveHost directive caused host header issues

Here's the virtual host configuration that finally worked on Server A:


<VirtualHost *:80>
    ServerName owncloud.mydomain.com
    
    ProxyPass / http://192.168.0.10/
    ProxyPassReverse / http://192.168.0.10/
    ProxyPreserveHost On
    
    # Important for preventing redirect loops
    ProxyRequests Off
    
    # Additional headers if needed
    RequestHeader set X-Forwarded-Proto "http"
    RequestHeader set X-Forwarded-Port "80"
</VirtualHost>

On the backend server (Server B), ensure these settings in httpd.conf or your virtual host:


<Directory "/var/www/owncloud">
    Options FollowSymLinks
    AllowOverride All
    Require all granted
    
    # Important for proxy setups
    SetEnvIf X-Forwarded-Proto https HTTPS=on
</Directory>

Make sure your DNS records point correctly:

  • owncloud.mydomain.com A record → Server A's public IP
  • Internal DNS or hosts file should resolve owncloud.mydomain.com to Server A's internal IP

If you encounter issues:

  1. Check Apache error logs on both servers
  2. Verify mod_proxy and mod_proxy_http are enabled: a2enmod proxy proxy_http
  3. Test connectivity between servers: curl -I http://192.168.0.10
  4. Ensure no conflicting virtual hosts or ServerName directives

When implementing this proxy setup:

  • Consider adding IP restrictions if servers are internet-facing
  • Implement SSL termination if needed (additional ProxyPass configuration required)
  • Monitor proxy performance and connection counts

If you prefer using paths instead of subdomains:


<VirtualHost *:80>
    ServerName mydomain.com
    
    ProxyPass /owncloud http://192.168.0.10/
    ProxyPassReverse /owncloud http://192.168.0.10/
    
    # Rest of your main site configuration
</VirtualHost>

This configuration maintains cleaner URL structures while still routing requests to the appropriate backend server.