# How to run Apache behind Nginx

On TurboStack, Apache runs as a backend behind nginx. Nginx handles the edge - Transport Layer Security (TLS), static files and
(if enabled) the Varnish cache - and forwards dynamic requests to Apache, which runs your application
and reads `.htaccess`. This gives you Apache's per-directory flexibility with Nginx's performance at
the front.

## How requests flow

1. A request arrives at Nginx (and Varnish, if enabled), which terminates HTTPS and serves cached or
   static content.
2. Dynamic requests are proxied to Apache, which runs the application and applies any
   [`.htaccess` rules](htaccess-overrides.md).

## Set it up

Choose Apache as the host's web server by setting `webserver: apache2` - see
[Configure Apache](configure.md). TurboStack wires Nginx in front of Apache for you; there is no
proxy configuration to write by hand.

## What you configure where

| Concern | Where |
| --- | --- |
| TLS, HTTP-to-HTTPS redirect, static files, caching (Varnish) | Nginx (managed by the platform) |
| Per-directory rules, rewrites, access control, headers | Apache `.htaccess` in your web root |

> [!IMPORTANT]
> Because Nginx (and Varnish) sit in front, the visitor's IP reaches Apache in the `X-Forwarded-For`
> header. IP-based rules in `.htaccess` must read that header behind Varnish - see
> [Use .htaccess overrides](htaccess-overrides.md).

## When to choose this setup

Choose Apache when your application depends on `.htaccess` rules that it ships and maintains itself.
If you do not need `.htaccess`, Nginx alone (`webserver: nginx`) is the lighter default - see
[Configure Nginx](../nginx/configure.md).

## Related

- [Configure Apache](configure.md)
- [Use .htaccess overrides](htaccess-overrides.md)
- [Configure Nginx](../nginx/configure.md)
