# Hosting Behind a Proxy or Cloudflare

> How facet and geocode request limits identify visitors, and the filters for proxies, CDNs, and limit tuning.

<!-- Sources: src/Support/ClientIpResolver.php; src/Facets/FacetRestEndpoint.php; src/Facets/FacetRateLimiter.php; src/Facets/FacetConcurrencyLimiter.php; src/Geocoding/GeocodeRestController.php; tests/Unit/Support/ClientIpResolverTest.php -->

# Hosting Behind a Proxy or Cloudflare

The public facet endpoint and the Map Address geocode endpoint limit requests per visitor IP address. Out of the box the visitor IP is the address of the direct connection (`REMOTE_ADDR`). That is the safe default: forwarded headers such as `X-Forwarded-For` can be set by anyone, so trusting them without a proxy in front of the site would let a visitor pick a fresh IP for every request and bypass the limits.

Behind a reverse proxy, load balancer, or CDN such as Cloudflare, `REMOTE_ADDR` is the proxy, so **all visitors share one limit bucket**. Busy sites then see `429 Too Many Requests` (`facet_rate_limited`, `facet_concurrency_exceeded`, `ome_geocode_rate_limited`) for ordinary traffic. Tell Oh My Etch which peers are your proxies so it can read the real visitor address.

## Default limits

| Limit                       | Default                                                       | Keyed by                    | Filter                                    |
| --------------------------- | ------------------------------------------------------------- | --------------------------- | ----------------------------------------- |
| Facet requests              | 60 per 60 seconds                                             | visitor IP + facet template | `ome_facets_rate_limit_config`            |
| Concurrent facet requests   | 3                                                             | visitor IP + facet template | `ome_facets_max_concurrent_requests`      |
| Geocode lookups             | 60 per 60 seconds                                             | visitor IP                  | `ome_geocode_rate_limit_config`           |
| Stripe PaymentIntents (Woo) | 200 per 10 minutes per visitor IP, 40 per 10 minutes per cart | visitor IP / cart token     | `ome_woo_stripe_intent_rate_limit_config` |

Facet limits do not run when `wp_get_environment_type()` is `local` or `development`.

## Trust your proxies

`ome_facets_trusted_proxies` returns the addresses of the proxies that connect to WordPress. Entries can be single IPv4/IPv6 addresses or CIDR ranges (`10.0.0.0/8`, `2001:db8::/32`). Malformed entries are ignored. The list applies to the facet, geocode and Stripe PaymentIntent endpoints.

Forwarded headers are read **only** when the direct peer matches this list. When nothing is configured, every forwarded header is ignored.

```php
add_filter( 'ome_facets_trusted_proxies', function (): array {
	return array(
		'10.0.0.0/8',   // Internal load balancer.
		'192.0.2.10',   // Single reverse proxy.
	);
} );
```

## Choose which headers carry the visitor IP

`ome_facets_client_ip_headers` lists the headers read from a trusted peer, in order. The default is `X-Forwarded-For` only. Each header may hold a comma-separated chain; Oh My Etch walks it from the right and returns the first address that is not itself a trusted proxy. If no header yields an address, `REMOTE_ADDR` is used.

### Cloudflare

Trust [Cloudflare's published IP ranges](https://www.cloudflare.com/ips/) and read `CF-Connecting-IP` first:

```php
add_filter( 'ome_facets_trusted_proxies', function (): array {
	return array(
		'173.245.48.0/20',
		'103.21.244.0/22',
		// ...the rest of https://www.cloudflare.com/ips-v4 and /ips-v6.
	);
} );

add_filter( 'ome_facets_client_ip_headers', function (): array {
	return array( 'CF-Connecting-IP', 'X-Forwarded-For' );
} );
```

Keep the range list current. If your host already restores the visitor address into `REMOTE_ADDR` (for example with `mod_remoteip` or nginx `real_ip`), you need neither filter.

## Override the resolved address

`ome_facets_client_ip` receives the resolved address and the endpoint context (`facets` or `geocode`) and runs last. Return a valid IP address; any other value is ignored and the resolved address is kept.

```php
add_filter( 'ome_facets_client_ip', function ( string $ip, string $context ): string {
	return $ip;
}, 10, 2 );
```

## Tune or disable limits

```php
// Allow 120 facet requests per minute per visitor and template.
add_filter( 'ome_facets_rate_limit_config', fn (): array => array( 'threshold' => 120, 'window' => 60 ) );

// Allow 5 concurrent facet requests per visitor and template.
add_filter( 'ome_facets_max_concurrent_requests', fn (): int => 5 );

// Allow 30 geocode lookups per minute per visitor.
add_filter( 'ome_geocode_rate_limit_config', fn (): array => array( 'threshold' => 30, 'window' => 60 ) );

// Turn facet rate and concurrency limits off (for example behind a WAF that limits already).
add_filter( 'ome_facets_enforce_request_limits', '__return_false' );
```

Facet responses carry `X-RateLimit-Remaining` and `X-RateLimit-Reset` headers while limits are enforced. Requests with a malformed template ID are rejected before they reach the limiter.

Related filters: `ome_facets_environment_type` overrides the environment type used to skip facet limits, and `ome_facets_allow_public_request` can block public facet requests entirely.
