|
| 1 | +--- |
| 2 | +head.title: 'Configuring Trusted Proxies - Docker PHP - Server Side Up' |
| 3 | +description: 'Learn how to configure trusted proxies to get accurate client IP addresses when running behind load balancers, CDNs, or reverse proxies.' |
| 4 | +layout: docs |
| 5 | +--- |
| 6 | + |
| 7 | +::lead-p |
| 8 | +When your application runs behind a load balancer, CDN, or reverse proxy, the web server sees the proxy's IP address instead of the actual visitor's IP. Trusted proxy configuration tells your web server which proxies to trust for forwarding the real client IP. |
| 9 | +:: |
| 10 | + |
| 11 | +## Why Trusted Proxies Matter |
| 12 | + |
| 13 | +Without proper trusted proxy configuration, your application will: |
| 14 | +- Log the proxy's IP address instead of the visitor's real IP |
| 15 | +- Have incorrect geolocation data |
| 16 | +- Potentially break rate limiting or IP-based security rules |
| 17 | +- Show incorrect information in Laravel's `request()->ip()` or similar methods |
| 18 | + |
| 19 | +::warning |
| 20 | +Never trust all IP addresses as proxies. Only trust specific proxy IPs that you control or know are legitimate (like your CDN provider). |
| 21 | +:: |
| 22 | + |
| 23 | +## Available Options |
| 24 | + |
| 25 | +Set the `TRUSTED_PROXY` environment variable to one of the following values: |
| 26 | + |
| 27 | +| Value | Description | |
| 28 | +|-------|-------------| |
| 29 | +| `cloudflare` (default) | Trusts [Cloudflare's IP ranges](https://www.cloudflare.com/ips/){target="_blank"} **+ local Docker networks** using the `CF-Connecting-IP` header | |
| 30 | +| `sucuri` | Trusts [Sucuri's IP ranges](https://docs.sucuri.net/website-firewall/sucuri-firewall-troubleshooting-guide/){target="_blank"} **+ local Docker networks** using the `X-Forwarded-For` header | |
| 31 | +| `local` | Trusts only private/local network ranges (Docker networks, localhost) using the `X-Forwarded-For` header | |
| 32 | +| `off` | Disables trusted proxy configuration entirely | |
| 33 | + |
| 34 | +::tip |
| 35 | +**You don't need to choose between your CDN and local proxies.** All options (except `off`) automatically include Docker's internal network ranges (`10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`). This means if you're behind Cloudflare *and* running a reverse proxy like Traefik or Caddy in Docker, just use `cloudflare` — it already trusts both. |
| 36 | +:: |
| 37 | + |
| 38 | +## Quick Start |
| 39 | + |
| 40 | +### Using Cloudflare (Default) |
| 41 | + |
| 42 | +If you're using Cloudflare as your CDN/proxy, you don't need to change anything. The default configuration already trusts Cloudflare's IP ranges: |
| 43 | + |
| 44 | +```yml [compose.yml] |
| 45 | +services: |
| 46 | + php: |
| 47 | + image: serversideup/php:8.5-fpm-nginx |
| 48 | + ports: |
| 49 | + - "80:8080" |
| 50 | + volumes: |
| 51 | + - .:/var/www/html |
| 52 | + # TRUSTED_PROXY defaults to "cloudflare" |
| 53 | +``` |
| 54 | + |
| 55 | +### Using Sucuri |
| 56 | + |
| 57 | +If you're using Sucuri as your web application firewall: |
| 58 | + |
| 59 | +```yml [compose.yml]{8} |
| 60 | +services: |
| 61 | + php: |
| 62 | + image: serversideup/php:8.5-fpm-nginx |
| 63 | + ports: |
| 64 | + - "80:8080" |
| 65 | + volumes: |
| 66 | + - .:/var/www/html |
| 67 | + environment: |
| 68 | + TRUSTED_PROXY: "sucuri" |
| 69 | +``` |
| 70 | +
|
| 71 | +### Local/Docker Networks Only |
| 72 | +
|
| 73 | +If you're running behind your own reverse proxy (like Traefik or Caddy) on the same Docker network: |
| 74 | +
|
| 75 | +```yml [compose.yml]{8} |
| 76 | +services: |
| 77 | + php: |
| 78 | + image: serversideup/php:8.5-fpm-nginx |
| 79 | + ports: |
| 80 | + - "80:8080" |
| 81 | + volumes: |
| 82 | + - .:/var/www/html |
| 83 | + environment: |
| 84 | + TRUSTED_PROXY: "local" |
| 85 | +``` |
| 86 | +
|
| 87 | +### Disabling Trusted Proxies |
| 88 | +
|
| 89 | +If you want to handle proxy configuration yourself or your application is directly exposed to the internet: |
| 90 | +
|
| 91 | +```yml [compose.yml]{8} |
| 92 | +services: |
| 93 | + php: |
| 94 | + image: serversideup/php:8.5-fpm-nginx |
| 95 | + ports: |
| 96 | + - "80:8080" |
| 97 | + volumes: |
| 98 | + - .:/var/www/html |
| 99 | + environment: |
| 100 | + TRUSTED_PROXY: "off" |
| 101 | +``` |
| 102 | +
|
| 103 | +## Laravel Configuration |
| 104 | +
|
| 105 | +While the Docker images handle the web server's trusted proxy configuration, Laravel also needs to know about trusted proxies at the application level. |
| 106 | +
|
| 107 | +Laravel includes a `TrustProxies` middleware that you should configure in your application. For most setups using our images, you can trust all proxies since the web server has already validated them: |
| 108 | + |
| 109 | +```php [bootstrap/app.php] |
| 110 | +->withMiddleware(function (Middleware $middleware) { |
| 111 | + $middleware->trustProxies(at: '*'); |
| 112 | +}) |
| 113 | +``` |
| 114 | + |
| 115 | +Or if you prefer more control, configure specific headers: |
| 116 | + |
| 117 | +```php [bootstrap/app.php] |
| 118 | +->withMiddleware(function (Middleware $middleware) { |
| 119 | + $middleware->trustProxies( |
| 120 | + at: '*', |
| 121 | + headers: Request::HEADER_X_FORWARDED_FOR | |
| 122 | + Request::HEADER_X_FORWARDED_HOST | |
| 123 | + Request::HEADER_X_FORWARDED_PORT | |
| 124 | + Request::HEADER_X_FORWARDED_PROTO |
| 125 | + ); |
| 126 | +}) |
| 127 | +``` |
| 128 | + |
| 129 | +::tip |
| 130 | +For Cloudflare specifically, Laravel can use the `CF-Connecting-IP` header. The `HEADER_X_FORWARDED_FOR` setting works because Cloudflare also sets this header. |
| 131 | +:: |
| 132 | + |
| 133 | +:u-button{to="https://laravel.com/docs/requests#trusting-proxies" target="_blank" label="Learn more about Laravel's TrustProxies middleware" aria-label="Learn more about Laravel's TrustProxies middleware" size="md" color="primary" variant="outline" trailing-icon="i-lucide-arrow-right" class="font-bold ring ring-inset ring-blue-600 text-blue-600 hover:ring-blue-500 hover:text-blue-500"} |
| 134 | + |
| 135 | +## How It Works |
| 136 | + |
| 137 | +### Cloudflare |
| 138 | +When `TRUSTED_PROXY=cloudflare`: |
| 139 | +- Trusts Cloudflare's published IPv4 and IPv6 ranges |
| 140 | +- Uses the `CF-Connecting-IP` header to get the real client IP |
| 141 | +- Includes Docker network ranges for container-to-container communication |
| 142 | + |
| 143 | +### Sucuri |
| 144 | +When `TRUSTED_PROXY=sucuri`: |
| 145 | +- Trusts Sucuri's WAF IP ranges |
| 146 | +- Uses the `X-Forwarded-For` header to get the real client IP |
| 147 | +- Includes Docker network ranges for container-to-container communication |
| 148 | + |
| 149 | +### Local |
| 150 | +When `TRUSTED_PROXY=local`: |
| 151 | +- Trusts only private network ranges (RFC 1918) |
| 152 | +- Trusts localhost and IPv6 loopback addresses |
| 153 | +- Uses the `X-Forwarded-For` header |
| 154 | +- Perfect for setups with your own reverse proxy on the same network |
| 155 | + |
| 156 | +## Security Considerations |
| 157 | + |
| 158 | +::caution |
| 159 | +Incorrectly configuring trusted proxies can allow attackers to spoof their IP address by sending fake headers. Only trust IP ranges that you know are legitimate proxies. |
| 160 | +:: |
| 161 | + |
| 162 | +- **Don't trust all IPs**: Never set your web server to trust `X-Forwarded-For` from any IP address |
| 163 | +- **Keep configurations updated**: CDN IP ranges can change over time. Our images are updated regularly, but if security is critical, verify the ranges match your provider's published list |
| 164 | +- **Use the right option**: If you're not using Cloudflare or Sucuri, don't use those options. Use `local` if you have your own reverse proxy, or `off` if you don't need proxy trust |
| 165 | + |
| 166 | +## Troubleshooting |
| 167 | + |
| 168 | +### Still seeing proxy IP instead of real IP |
| 169 | + |
| 170 | +1. **Check your CDN/proxy is in the trusted list**: Verify your proxy's IP is within the trusted ranges for your chosen option |
| 171 | +2. **Check the header being used**: Different proxies use different headers. Cloudflare uses `CF-Connecting-IP`, while most others use `X-Forwarded-For` |
| 172 | +3. **Check Laravel configuration**: Remember to also configure Laravel's `TrustProxies` middleware |
| 173 | + |
| 174 | +### Application works but logs show wrong IP |
| 175 | + |
| 176 | +Your web server might be configured correctly, but your application framework might need separate configuration. See the [Laravel Configuration](#laravel-configuration) section above. |
| 177 | + |
| 178 | +### Need to trust a different proxy provider |
| 179 | + |
| 180 | +If your proxy provider isn't listed, you can create a custom configuration. See [Custom Trusted Proxy Configuration](#custom-trusted-proxy-configuration) below. |
| 181 | + |
| 182 | +## Custom Trusted Proxy Configuration |
| 183 | + |
| 184 | +If you're using a proxy provider that isn't included (like AWS ALB, Fastly, or a custom load balancer), you can create your own trusted proxy configuration. |
| 185 | + |
| 186 | +### Two Ways to Add Custom Configs |
| 187 | + |
| 188 | +| Method | Best For | How It Works | |
| 189 | +|--------|----------|--------------| |
| 190 | +| **Dockerfile (recommended)** | Production deployments | Bakes the config into your image | |
| 191 | +| **Volume mount** | Local development | Mount the file at runtime | |
| 192 | + |
| 193 | +The examples below use the Dockerfile approach. To use a volume mount instead, simply replace the `COPY` instruction with a volume mount in your `compose.yml`: |
| 194 | + |
| 195 | +```yml [compose.yml] |
| 196 | +volumes: |
| 197 | + # Mount syntax: ./local/path:/container/path:ro |
| 198 | + - ./docker/trusted-proxy/aws-alb.conf:/etc/nginx/trusted-proxy/aws-alb.conf:ro |
| 199 | +``` |
| 200 | + |
| 201 | +| Variation | Config Path | |
| 202 | +|-----------|-------------| |
| 203 | +| `fpm-nginx` | `/etc/nginx/trusted-proxy/{name}.conf` | |
| 204 | +| `fpm-apache` | `/etc/apache2/trusted-proxy/{name}.conf` | |
| 205 | +| `frankenphp` | `/etc/frankenphp/trusted-proxy/{name}.caddyfile` | |
| 206 | + |
| 207 | +::tip |
| 208 | +Always include Docker's internal network ranges (`10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`) so container-to-container communication works correctly. |
| 209 | +:: |
| 210 | + |
| 211 | +::warning |
| 212 | +Keep your custom IP ranges up to date. Cloud providers periodically update their IP ranges. |
| 213 | +:: |
| 214 | + |
| 215 | +### FPM-NGINX Custom Configuration |
| 216 | + |
| 217 | +::code-tree{defaultValue="Dockerfile"} |
| 218 | + |
| 219 | +```dockerfile [Dockerfile] |
| 220 | +FROM serversideup/php:8.5-fpm-nginx |
| 221 | +
|
| 222 | +# Copy our custom trusted proxy configuration |
| 223 | +COPY --chmod=644 docker/trusted-proxy/aws-alb.conf /etc/nginx/trusted-proxy/aws-alb.conf |
| 224 | +``` |
| 225 | + |
| 226 | +```yml [compose.yml] |
| 227 | +services: |
| 228 | + php: |
| 229 | + build: |
| 230 | + context: . |
| 231 | + dockerfile: Dockerfile |
| 232 | + ports: |
| 233 | + - "80:8080" |
| 234 | + volumes: |
| 235 | + - .:/var/www/html |
| 236 | + environment: |
| 237 | + TRUSTED_PROXY: "aws-alb" |
| 238 | +``` |
| 239 | + |
| 240 | +```conf [docker/trusted-proxy/aws-alb.conf] |
| 241 | +## |
| 242 | +# AWS ALB - Trusted Proxy (example) |
| 243 | +## |
| 244 | +
|
| 245 | +# Configure docker networks and loopback addresses |
| 246 | +set_real_ip_from 10.0.0.0/8; |
| 247 | +set_real_ip_from 172.16.0.0/12; |
| 248 | +set_real_ip_from 192.168.0.0/16; |
| 249 | +set_real_ip_from 127.0.0.1/8; |
| 250 | +set_real_ip_from ::1; |
| 251 | +set_real_ip_from fd00::/8; |
| 252 | +
|
| 253 | +# Add your proxy provider's IP ranges |
| 254 | +# Example: AWS us-east-1 ranges (check AWS docs for current IPs) |
| 255 | +set_real_ip_from 3.0.0.0/8; |
| 256 | +
|
| 257 | +# Set the header your proxy uses |
| 258 | +real_ip_header X-Forwarded-For; |
| 259 | +``` |
| 260 | +:: |
| 261 | + |
| 262 | +### FPM-Apache Custom Configuration |
| 263 | + |
| 264 | +::code-tree{defaultValue="Dockerfile"} |
| 265 | + |
| 266 | +```dockerfile [Dockerfile] |
| 267 | +FROM serversideup/php:8.5-fpm-apache |
| 268 | +
|
| 269 | +# Copy our custom trusted proxy configuration |
| 270 | +COPY --chmod=644 docker/trusted-proxy/aws-alb.conf /etc/apache2/trusted-proxy/aws-alb.conf |
| 271 | +``` |
| 272 | + |
| 273 | +```yml [compose.yml] |
| 274 | +services: |
| 275 | + php: |
| 276 | + build: |
| 277 | + context: . |
| 278 | + dockerfile: Dockerfile |
| 279 | + ports: |
| 280 | + - "80:8080" |
| 281 | + volumes: |
| 282 | + - .:/var/www/html |
| 283 | + environment: |
| 284 | + TRUSTED_PROXY: "aws-alb" |
| 285 | +``` |
| 286 | + |
| 287 | +```conf [docker/trusted-proxy/aws-alb.conf] |
| 288 | +## |
| 289 | +# AWS ALB - Trusted Proxy (example) |
| 290 | +## |
| 291 | +
|
| 292 | +# Set the header your proxy uses |
| 293 | +RemoteIPHeader X-Forwarded-For |
| 294 | +
|
| 295 | +# Configure docker networks |
| 296 | +RemoteIPTrustedProxy 10.0.0.0/8 |
| 297 | +RemoteIPTrustedProxy 172.16.0.0/12 |
| 298 | +RemoteIPTrustedProxy 192.168.0.0/16 |
| 299 | +RemoteIPTrustedProxy 127.0.0.1/8 |
| 300 | +RemoteIPTrustedProxy ::1 |
| 301 | +RemoteIPTrustedProxy fd00::/8 |
| 302 | +
|
| 303 | +# Add your proxy provider's IP ranges |
| 304 | +# Example: AWS us-east-1 ranges (check AWS docs for current IPs) |
| 305 | +RemoteIPTrustedProxy 3.0.0.0/8 |
| 306 | +``` |
| 307 | +:: |
| 308 | + |
| 309 | +### FrankenPHP Custom Configuration |
| 310 | + |
| 311 | +::code-tree{defaultValue="Dockerfile"} |
| 312 | + |
| 313 | +```dockerfile [Dockerfile] |
| 314 | +FROM serversideup/php:8.5-frankenphp |
| 315 | +
|
| 316 | +# Copy our custom trusted proxy configuration |
| 317 | +COPY --chmod=644 docker/trusted-proxy/aws-alb.caddyfile /etc/frankenphp/trusted-proxy/aws-alb.caddyfile |
| 318 | +``` |
| 319 | + |
| 320 | +```yml [compose.yml] |
| 321 | +services: |
| 322 | + php: |
| 323 | + build: |
| 324 | + context: . |
| 325 | + dockerfile: Dockerfile |
| 326 | + ports: |
| 327 | + - "80:8080" |
| 328 | + volumes: |
| 329 | + - .:/var/www/html |
| 330 | + environment: |
| 331 | + TRUSTED_PROXY: "aws-alb" |
| 332 | +``` |
| 333 | + |
| 334 | +```caddyfile [docker/trusted-proxy/aws-alb.caddyfile] |
| 335 | +servers { |
| 336 | + # Trust Docker/private networks + loopback |
| 337 | + trusted_proxies static \ |
| 338 | +10.0.0.0/8 \ |
| 339 | +172.16.0.0/12 \ |
| 340 | +192.168.0.0/16 \ |
| 341 | +127.0.0.1/8 \ |
| 342 | +::1 \ |
| 343 | +fd00::/8 \ |
| 344 | +3.0.0.0/8 |
| 345 | +
|
| 346 | + # Set the header your proxy uses |
| 347 | + client_ip_headers X-Forwarded-For |
| 348 | +} |
| 349 | +``` |
| 350 | +:: |
0 commit comments