Spring Boot with embedded Tomcat behind Apache proxy
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Running Spring Boot with embedded Tomcat behind Apache HTTP Server is a common production pattern. Apache handles TLS termination, request routing, and static concerns, while Tomcat serves the application. The main challenge is preserving original request context, especially scheme, host, and client IP, so redirects, generated links, and security logic stay correct. Proper forwarding headers and timeout alignment are essential for stable behavior. This guide shows a practical reverse-proxy setup, the Spring Boot properties that matter, and checks you should run before shipping to production.
Core Sections
Configure Apache as reverse proxy
A minimal Apache virtual host can forward traffic to Tomcat on localhost.
Enable modules such as proxy, proxy_http, headers, and ssl.
Make Spring Boot trust forwarded headers
Spring needs to read forwarded headers so URL generation and redirect logic use public HTTPS endpoints.
For modern Boot versions:
For older setups, use Tomcat remote IP configuration if needed:
When this is wrong, apps often redirect from https to http unexpectedly.
Preserve client IP and security context
If you rely on rate limiting, audit logs, or geo rules, ensure X-Forwarded-For is propagated and trusted only from known proxies.
Use this only when needed and pair it with network-level restrictions so clients cannot spoof forwarding headers directly.
Align operational settings
Proxy and app timeouts should be consistent. If Apache times out sooner than Tomcat, users see proxy errors while the backend is still processing. Also configure max request size and upload buffers on both sides to avoid inconsistent failures.
For troubleshooting, compare:
- Apache access/error logs
- Spring Boot access logs
- Upstream response timing metrics
This quickly reveals whether failures occur at proxy, network, or application layer.
Common Pitfalls
- Forgetting
X-Forwarded-Protoand getting incorrect absolute URLs or insecure redirect loops. - Trusting forwarded headers from untrusted networks, which can enable spoofed client IP behavior.
- Mismatched timeout settings between Apache and Tomcat, causing intermittent 502 or 504 responses.
- Using
ProxyPassrules that accidentally bypass static assets or health endpoints. - Disabling host preservation and breaking virtual-host-aware application logic.
Production Readiness Check
Before closing the task, run a short validation loop on representative inputs and one intentional failure case. Confirm that your code path behaves correctly for normal data, empty data, and malformed data. Capture at least one measurable signal such as runtime, memory use, or error rate, then compare it to your baseline so regressions are visible. Keep this check lightweight so it can run in local development and CI without slowing feedback too much. A simple checklist plus one executable smoke test prevents most regressions after refactors and library upgrades.
Summary
Spring Boot behind Apache proxy is robust when request context is forwarded correctly and both layers are configured consistently. Set Apache proxy headers, tell Spring Boot to honor them, and verify client IP and scheme handling in logs. Treat timeouts and request limits as an end-to-end contract rather than isolated settings. With these fundamentals in place, embedded Tomcat behind Apache is a reliable deployment model for many Java web services. Add one automated smoke test that asserts forwarded scheme and host values so proxy regressions are caught before release.
Related reading
- Spring Cloud or Spring Boot? what is right spring project for developing Biz API's?
- Spring Data Elastic Search vs Java High Level REST Client
- Spring Data JPA - could not initialize proxy - no Session - With Methods marked as transactional
- Spring Data JPA Unable to locate Attribute with the given name
- Spring Boot with Kotlin - Value annotation not working as expected
- Spring Boot with redirecting with single page angular2
- Spring Kafka - Event sourcing - Example of how to query some entity state using Kafka + KafkaStreams API
- Spring Kafka Poll for new messages instead of being notified using `onMessage`

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.