When using Apache's ProxyPass to route requests to backend applications, error document handling requires special consideration. The standard ErrorDocument directive doesn't automatically intercept 404 responses from proxied applications because the response comes from the backend server rather than Apache itself.
To properly handle this scenario, we need to:
- Preserve the original request URL structure
- Intercept backend 404 responses
- Maintain proper HTTP status codes
- Allow custom error page content
The critical directive you're missing is ProxyErrorOverride. Here's the corrected configuration:
<VirtualHost *:80>
ServerName servername
DocumentRoot /somepath/
ProxyPass / http://localhost:8080/someapp/
ProxyPassReverse / http://localhost:8080/someapp/
ProxyErrorOverride On
ErrorDocument 404 /error.html
</VirtualHost>
For more complex scenarios, consider these additional directives:
# Only override errors for specific status codes
ProxyErrorOverride 404 500
# Different error documents for different status codes
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html
# Location-based error handling
<Location /special/>
ProxyErrorOverride Off
</Location>
After implementing the solution:
- Test with direct backend URLs to verify the backend returns 404
- Test through the proxy to confirm custom error page appears
- Check HTTP headers to ensure proper status code propagation
If the solution isn't working:
- Verify ErrorDocument path is accessible (try accessing it directly)
- Check Apache error logs for "File does not exist" messages
- Ensure ProxyErrorOverride is in the correct VirtualHost context
- Test with curl -v to inspect full HTTP headers
For high-traffic sites:
- Place error documents in memory (RAM disk)
- Consider caching backend error responses
- Keep error pages lightweight (avoid complex assets)
When setting up Apache as a reverse proxy with ProxyPass, many developers encounter an unexpected behavior: the ErrorDocument directive doesn't intercept 404 errors coming from the backend application. This happens because Apache treats successful proxy connections differently from failed resource access.
The default Apache behavior considers a successful proxy connection (even if the backend returns 404) as a successful request completion. Therefore, the ErrorDocument directive for 404 gets bypassed. Here's what typically happens in the request flow:
Client → Apache (ProxyPass) → Backend (404)
Apache receives the 404 but treats it as valid response from backend
The key to solving this is the ProxyErrorOverride
directive. When enabled, it allows Apache to intercept error codes from the backend and apply local ErrorDocument handling:
<VirtualHost *:80>
ServerName servername
DocumentRoot /somepath/
ProxyPass / http://localhost:8080/someapp/
ProxyPassReverse / http://localhost:8080/someapp/
ProxyErrorOverride On
ErrorDocument 404 /error.html
</VirtualHost>
For more complex scenarios, you might want to consider these additional configurations:
# Only override specific error codes
ProxyErrorOverride 404 500 503
# Custom error page based on request URI
ErrorDocument 404 /errors/404.php
# Dynamic error handling with ErrorDocument
ErrorDocument 404 "<h1>Not Found</h1><p>The requested URL %{REQUEST_URI} was not found</p>"
After implementing these changes, verify your setup works correctly:
- Restart Apache:
sudo apachectl restart
- Test with curl:
curl -I http://yourserver/nonexistent-page
- Check the response headers for your custom error page
While ProxyErrorOverride solves the functional requirement, be aware that:
- It adds minimal overhead as Apache needs to inspect backend responses
- The backend 404 response must be properly formatted (HTTP status code in headers)
- For high-traffic sites, consider caching error pages