How to handle relative URLs correctly with a reverse proxy in Apache

Better Stack Team
Updated on November 9, 2023

When using a reverse proxy in Apache, handling relative URLs correctly is crucial to ensure that requests are routed properly and content is displayed as intended. Here are steps to handle relative URLs correctly with a reverse proxy in Apache:

Enable mod_proxy and mod_proxy_http:

  1. Ensure that the necessary Apache modules are enabled:

     
    sudo a2enmod proxy
    sudo a2enmod proxy_http
    sudo systemctl restart apache2
    
    

Configure the Reverse Proxy:

  1. Open your Apache configuration file:

     
    sudo nano /etc/apache2/sites-available/your-site.conf
    
    
  2. Set up the reverse proxy configuration, specifying the ProxyPass and ProxyPassReverse directives:

     
    <VirtualHost *:80>
        ServerName your-domain.com
    
        ProxyPass /app1/ <http://internal-server:8080/app1/>
        ProxyPassReverse /app1/ <http://internal-server:8080/app1/>
    </VirtualHost>
    
    
 
- `ProxyPass` defines the mapping of the URL path to the backend server.
- `ProxyPassReverse` ensures that the URLs in the response headers are also rewritten to match the proxy.

Handle Relative URLs:

  1. Use the ProxyHTMLURLMap directive to handle rewriting of relative URLs in HTML content:

     
    <Location /app1/>
        ProxyPass <http://internal-server:8080/app1/>
        ProxyPassReverse <http://internal-server:8080/app1/>
        SetOutputFilter proxy-html
        ProxyHTMLURLMap <http://internal-server:8080/app1/> /app1/
    </Location>
    
    

Additional Configuration (If Required):

  1. Rewrite Rules: You might need additional mod_rewrite rules to adjust URLs if the application requires specific transformations for internal paths.
  2. SSL Termination: If your proxy server terminates SSL and the backend server uses HTTP, ensure that URL paths are correctly handled in both secure and insecure contexts.
  3. Caching and Compression: Be cautious with caching and compression configurations. They can affect how resources are served and how URLs are handled.

Testing:

  1. After making these changes, restart or reload Apache:

     
    sudo systemctl restart apache2
    
    
  2. Test by accessing the application through the configured URL. Check that pages load correctly, and relative URLs point to the right resources.

By configuring the reverse proxy correctly and handling relative URLs, you ensure that content, including internal links and resources, is correctly mapped and displayed when accessed through the proxy. Adjustments might be necessary based on the specific requirements of the application being proxied.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github