Apache reverse proxy with subfolder

2 min read 07-10-2024
Apache reverse proxy with subfolder


Mastering Apache Reverse Proxy with Subfolders: A Comprehensive Guide

The Problem: You want to host multiple websites or applications under a single domain, with each site accessible through a unique subfolder. A classic example: your main website at example.com and a separate blog residing at example.com/blog. This setup simplifies administration and streamlines user access, but how do you effectively route traffic to the right location? Enter the Apache Reverse Proxy, a powerful tool that seamlessly handles this task.

Scenario and Code: Imagine you have two applications:

  • Main Website: Located at /var/www/html and accessible at http://localhost:8080/
  • Blog: Located at /var/www/blog and accessible at http://localhost:8081/

Here's the initial Apache configuration (in /etc/apache2/sites-available/example.com.conf):

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # Proxy for blog
    ProxyPreserveHost On
    ProxyRequests Off
    <Location /blog>
        ProxyPass http://localhost:8081/
        ProxyPassReverse http://localhost:8081/
    </Location>
</VirtualHost>

Understanding the Code:

  • <VirtualHost *:80>: Defines a virtual host for example.com on port 80.
  • ServerName example.com: Sets the server name for the virtual host.
  • DocumentRoot /var/www/html: Sets the root directory for the main website.
  • <Directory /var/www/html>: Defines directory-level configurations.
  • ProxyPreserveHost On: Ensures the original host header is preserved during proxying.
  • ProxyRequests Off: Disables client-side proxying.
  • <Location /blog>: This block defines the specific rule for the /blog subfolder.
  • ProxyPass http://localhost:8081/: Directs requests to /blog to the blog application at http://localhost:8081.
  • ProxyPassReverse http://localhost:8081/: Ensures the reverse proxy sends the correct location headers, preventing browser caching issues.

Key Insights and Considerations:

  • Clean URLs: By using a subfolder like /blog, you maintain clean URLs (e.g., example.com/blog/article-title) instead of resorting to separate domain names.
  • Security: Reverse proxying can enhance security by adding a layer of protection between your applications and the public internet.
  • Flexibility: This setup allows you to easily manage and deploy multiple applications under a single domain, making it ideal for larger projects.
  • Caching: You can leverage Apache's caching mechanisms to further improve performance by storing frequently accessed content closer to the user.
  • Load Balancing: For applications with high traffic, you can configure Apache to distribute requests across multiple backend servers.

Additional Value:

  • Error Handling: Implement error pages and logging to track and troubleshoot any issues.
  • SSL/TLS: Secure your site with SSL/TLS certificates for increased security and user trust.
  • Customizations: Explore Apache's vast configuration options to tailor your reverse proxy setup to specific needs, including custom headers, authentication, and more.

Resources:

Conclusion:

Mastering Apache reverse proxy with subfolders is a valuable skill for any web developer. By following this guide and understanding the underlying concepts, you can easily implement a robust and scalable solution for hosting multiple applications under a single domain. This setup provides a clean user experience while enhancing security and flexibility in your web infrastructure.