Scrivio Interview library
ConceptFoundations6 min read · updated 13 November 2026

A reverse proxy sits in front of servers and receives client requests on their behalf; a forward proxy sits in front of clients and sends their requests out. An API gateway is a reverse proxy with API features such as authentication, rate limits and routing per API.

What does a reverse proxy such as NGINX or Envoy do for the Java services behind it, and what does an API gateway add on top?

A reverse proxy such as NGINX or Envoy terminates TLS in one place, keeps pooled connections to the backends, buffers slow clients so they do not hold Java threads, compresses and caches, routes by host and path, balances load, enforces timeouts and size limits, and adds forwarded headers with the client's address and protocol. An API gateway is a reverse proxy with API concerns on top: authentication and token checks, rate limits and quotas per client, request validation, version routing, API keys and per-API analytics. Business logic stays in the services.

Why this question is asked

Almost every Java service in production sits behind some proxy, and a lot of behaviour that developers attribute to their application, timeouts, redirects to the wrong host, rejected uploads, actually comes from that layer. Interviewers ask this to see whether we understand what the proxy is doing for us and where the line between proxy, gateway and service should be. In this answer, we will recap reverse and forward proxies, go through what a reverse proxy does for the services behind it with a small configuration, look at forwarded headers, and then see what an API gateway adds.

1A Quick Recap: Reverse and Forward Proxies

A proxy is a server that sits in the middle of a conversation and talks to each side on behalf of the other. A forward proxy sits in front of clients: a company's outbound proxy, for example, sends employees' requests to the internet. A reverse proxy sits in front of servers: clients connect to it believing it is the service, and it forwards each request to one of the real servers behind it. The clients never see the backend addresses.

Because every request passes through the reverse proxy, it is the natural place for work that is the same for every request and every service. That is the theme of everything below.

2What the Proxy Does for the Java Services

The first job is TLS termination. The proxy holds the certificates and does the encryption, so certificate renewal and cipher choices are managed in one place rather than in every service's keystore. Behind it, the proxy keeps a pool of connections to the backends and reuses them, so ten thousand clients do not mean ten thousand connections to Tomcat.

The second job is buffering. A client on a slow mobile network may take ten seconds to send a request body or to read a large response. The proxy reads the whole request first and sends it to the backend at local network speed, and takes the whole response quickly and trickles it out to the client itself. A Java thread is busy only for the real work. The proxy can also compress responses and serve or cache static content.

The third job is traffic control: routing by host name and path to different backends, load balancing across instances, and enforcing timeouts and limits on request body size and header size, which protects the services from oversized or slow requests.

Java
upstream orders {
server 10.0.1.11:8080;
server 10.0.1.12:8080;
keepalive 64; # pooled connections to the backends
}
server {
listen 443 ssl;
server_name api.example.com;
ssl_certificate /etc/tls/api.pem; # TLS ends here
ssl_certificate_key /etc/tls/api.key;
client_max_body_size 10m; # larger uploads get 413
gzip on;
location /orders/ {
proxy_pass http://orders; # round robin by default
proxy_http_version 1.1;
proxy_set_header Connection ""; # needed for upstream keepalive
proxy_read_timeout 5s;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

Notice the last three lines. Once the proxy sits in the middle, the backend sees the proxy as its client, which brings us to forwarded headers.

3Forwarded Headers: Telling the Service Who the Client Is

Without help, the Java service sees the proxy's address as the client address and plain HTTP as the protocol, because TLS ended at the proxy. Then logs show the wrong client IP, rate limits by IP limit the proxy, and the application builds redirect URLs with http:// and the internal host name. The proxy fixes this by adding headers: X-Forwarded-For with the client's address, X-Forwarded-Proto with the original scheme, X-Forwarded-Host or the original Host header, or the standard Forwarded header that carries all of them.

The application must then be told to trust these headers, and only from the proxy, because a client could send them itself. In Spring Boot, setting server.forward-headers-strategy to native lets Tomcat apply them through its RemoteIpValve, and framework applies them through Spring's ForwardedHeaderFilter.

Properties
# application.properties
server.forward-headers-strategy=native
# request.getRemoteAddr() -> 198.51.100.7 (the client), not 10.0.0.5 (the proxy)
# request.getScheme() -> https, so redirects use https://api.example.com/...

4What an API Gateway Adds

An API gateway is a reverse proxy that understands APIs as products. On top of everything above, it handles authentication, for example validating a JWT or checking an API key before the request reaches a service, so a request with a bad token is rejected at the edge. It applies rate limits and quotas per client or per plan, such as 100 requests per second for a free key. It can validate requests against a schema, route by API version, transform small things such as headers, and record per-API analytics: who calls what, how often, and with what errors.

Examples include Spring Cloud Gateway, Kong, Envoy-based gateways and the managed gateways of the cloud providers. The layers diagram shows where each concern usually sits.

Step throughWhere each concern lives
  1. Clientbrowser, mobile app, partner
  2. Reverse proxyTLS, buffering, compression, routing, timeouts, size limits
  3. API gateway featuresauth, rate limits, quotas, validation, versions, analytics
  4. Servicebusiness rules, data ownership, domain decisions
  5. Data storesPostgreSQL, Redis, Kafka

5 layers, from the top down. Step through them one at a time.

Cross-cutting concerns at the edge, business logic in the services.

5Keep Business Logic in the Services

Gateways make it tempting to add a little logic: compute a discount in a plugin, merge two responses, decide access based on order status. Each of these moves domain rules into a shared component that one platform team owns and every API depends on, where it is hard to test and easy to break for everyone. The rule of thumb is that the gateway handles concerns that are the same for every API, identity, limits, routing, observability, and the services handle anything that needs to know what an order or a customer is.

6Follow-up questions

Which headers must the proxy add so the service builds correct redirect URLs?

The service needs the original scheme and host: X-Forwarded-Proto with https, and either the original Host header passed through or X-Forwarded-Host, plus X-Forwarded-Port if a non-standard port is used, or the single standard Forwarded header carrying proto and host. If a path prefix is stripped at the proxy, X-Forwarded-Prefix tells Spring the prefix to put back. Then the application must trust them, in Spring Boot with server.forward-headers-strategy set to native or framework, and the proxy should overwrite any such headers the client sent.

Do we still need a reverse proxy if the cloud load balancer terminates TLS?

Often the cloud load balancer is the reverse proxy for TLS, routing and health checks. Whether we add NGINX or Envoy behind it depends on what is missing, for example request buffering, per-route timeouts, or a service mesh sidecar. The functions matter more than the number of boxes.

7Conclusion

In this answer, we saw that a reverse proxy sits in front of servers and does work common to every request: TLS termination, pooled backend connections, buffering slow clients, compression and caching, routing, load balancing, timeouts and size limits, plus forwarded headers so the service knows the real client and scheme. An API gateway adds API concerns such as authentication, per-client rate limits and quotas, validation, version routing and analytics, while business logic stays in the services.

What to say in the interviewAbout 90 seconds aloud

Open with

A reverse proxy sits in front of our services and receives client requests on their behalf. For a Java service it does several useful things. It terminates TLS, so certificates and ciphers are managed in one place, and it keeps a pool of connections to the backends instead of one per client.

Then, if they want more

It buffers requests and responses, so a slow mobile client does not hold a Tomcat thread while it trickles bytes; it can also compress and serve or cache static content. It routes by host and path, balances load across instances, and enforces timeouts and limits on body and header size.

Because the backend now sees the proxy as the client, the proxy adds X-Forwarded-For, X-Forwarded-Proto and the host, and in Spring Boot I set server.forward-headers-strategy so the application uses them for client IPs and redirect URLs.

An API gateway is a reverse proxy with API features on top: authentication and token validation, rate limits and quotas per client or API key, request validation, routing by version, and per-API analytics, as in Spring Cloud Gateway or Kong.

The line I keep is that the gateway handles concerns that are the same for every API, and business logic stays in the services, because logic in a shared gateway is hard to test and can break every API at once.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does a reverse proxy such as NGINX or Envoy do for the Java services behind it, and...": https://scrivio-interview-library.netlify.app/architecture/load-balancing-networking/189b099125/