Trusted Proxies

Configure trusted proxies to correctly handle client IPs and headers when PteroCA is behind a proxy, load balancer, or CDN.

Overview

When PteroCA is behind a proxy (like Cloudflare, NGINX, or a load balancer), the application sees the proxy's IP address instead of the actual client IP. Configuring trusted proxies tells Symfony to trust certain headers from specific proxies.

When You Need This

Configure trusted proxies if you're using:

  • Cloudflare or other CDN

  • Reverse proxy (NGINX, Apache, HAProxy)

  • Load balancer

  • Docker with reverse proxy

  • Any proxy that forwards requests

Configuration

Step 1: Identify Proxy IPs

Determine which IP addresses should be trusted as proxies.

Cloudflare:

  • Use official IP ranges from https://www.cloudflare.com/ips/

NGINX/Apache reverse proxy:

  • Use the proxy server's IP address

  • If on same server: 127.0.0.1

  • If on different server: proxy's private/public IP

Load balancer:

  • Use all load balancer IP addresses

Docker:

  • Use Docker network gateway IP (usually 172.17.0.1 or similar)

Step 2: Configure in .env

Add the TRUSTED_PROXIES variable to your .env file:

Add one of the following configurations:

Single proxy:

Multiple proxies:

IP range (CIDR notation):

Cloudflare (all IPv4 ranges):

Trust all (testing only):

⚠️ Warning: Only use 0.0.0.0/0 for testing. This trusts all IPs and is insecure.

Step 3: Clear Cache

After updating .env, clear the cache:

Common Scenarios

Scenario 1: Cloudflare CDN

Setup: Domain → Cloudflare → Your server → PteroCA

Configuration:

Get current Cloudflare IPs:

Scenario 2: NGINX Reverse Proxy

Setup: Client → NGINX (proxy) → PteroCA

NGINX Configuration:

PteroCA Configuration:

Scenario 3: Docker with Reverse Proxy

Setup: Docker network with NGINX proxy container

Find Docker gateway IP:

Configuration:

Scenario 4: Load Balancer

Setup: Client → Load Balancer → Multiple PteroCA instances

Configuration:

Scenario 5: Multiple Layers

Setup: Cloudflare → Load Balancer → NGINX → PteroCA

Configuration:

Headers Trusted

When a proxy is trusted, Symfony trusts these headers from it:

  • X-Forwarded-For - Client IP address

  • X-Forwarded-Host - Original host

  • X-Forwarded-Proto - Protocol (HTTP/HTTPS)

  • X-Forwarded-Port - Original port

Verifying Configuration

Check Current IP Detection

Test with curl

Check Logs

Verify logs show correct client IPs:

Troubleshooting

Wrong IP Addresses in Logs

Symptom: Logs show proxy IP instead of client IP

Solutions:

  1. Verify TRUSTED_PROXIES is set

  2. Check proxy is sending headers

  3. Verify proxy IP is in trusted list

    • Get actual proxy IP from logs

    • Add to TRUSTED_PROXIES

  4. Clear cache

CSRF Token Errors

Symptom: "Invalid CSRF token" when behind proxy

Cause: Proxy not trusted, so client IP changes between requests

Solution:

See CSRF Protection for more details.

Rate Limiting Issues

Symptom: All users rate-limited together

Cause: Application sees all requests from proxy IP

Solution:

  • Configure TRUSTED_PROXIES correctly

  • Application will then use X-Forwarded-For for rate limiting

  • Each user has separate rate limit

Security Considerations

Only Trust Known Proxies

Don't trust all IPs:

Verify Proxy IPs Regularly

  1. Cloudflare IPs change - Check quarterly

  2. Proxy infrastructure changes - Update after infrastructure changes

  3. Remove old entries - Clean up unused proxy IPs

Use Specific Ranges

Validate Headers

Trusted proxies mean trusting their headers. Ensure:

  • Proxy is under your control

  • Proxy properly sanitizes headers

  • Proxy doesn't allow header injection

Advanced Configuration

Symfony Configuration

For more control, edit config/packages/framework.yaml:

Environment-Specific

Different proxies for different environments:

IPv6 Support

Include both IPv4 and IPv6 ranges:

Last updated