|
| 1 | +How to Configure Symfony to Work behind a Load Balancer or Reverse Proxy |
| 2 | +======================================================================== |
| 3 | + |
| 4 | +When you deploy your application, you may be behind a load balancer (e.g. |
| 5 | +an AWS Elastic Load Balancer) or a reverse proxy (e.g. Varnish for |
| 6 | +:doc:`caching</book/http_cache>`). |
| 7 | + |
| 8 | +For the most part, this doesn't cause any problems with Symfony. But, when |
| 9 | +a request passes through a proxy, certain request information is sent using |
| 10 | +special ``X-Forwarded-*`` headers. For example, instead of reading the ``REMOTE_ADDR`` |
| 11 | +header (which will now be the IP address of your reverse proxy), the user's |
| 12 | +true IP will be stored in an ``X-Forwarded-For`` header. |
| 13 | + |
| 14 | +.. tip:: |
| 15 | + |
| 16 | + If you're using Symfony's :ref:`AppCache<symfony-gateway-cache>` for caching, |
| 17 | + then you *are* using a reverse proxy with the IP address ``127.0.0.1``. |
| 18 | + You'll need to configure that address as a trusted proxy below. |
| 19 | + |
| 20 | +If you don't configure Symfony to look for these headers, you'll get incorrect |
| 21 | +information about the client's IP address, whether or not the client is connecting |
| 22 | +via HTTPS, the client's port and the hostname being requested. |
| 23 | + |
| 24 | +Solution: trusted_proxies |
| 25 | +------------------------- |
| 26 | + |
| 27 | +This is no problem, but you *do* need to tell Symfony that this is happening |
| 28 | +and which reverse proxy IP addresses will be doing this type of thing: |
| 29 | + |
| 30 | +.. configuration-block:: |
| 31 | + |
| 32 | + .. code-block:: yaml |
| 33 | +
|
| 34 | + # app/config/config.yml |
| 35 | + # ... |
| 36 | + framework: |
| 37 | + trusted_proxies: [192.0.0.1, 10.0.0.0/8] |
| 38 | +
|
| 39 | + .. code-block:: xml |
| 40 | +
|
| 41 | + <!-- app/config/config.xml --> |
| 42 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 43 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 44 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 45 | + xmlns:framework="http://symfony.com/schema/dic/symfony" |
| 46 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd |
| 47 | + http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> |
| 48 | +
|
| 49 | + <framework:config trusted-proxies="192.0.0.1, 10.0.0.0/8"> |
| 50 | + <!-- ... --> |
| 51 | + </framework> |
| 52 | + </container> |
| 53 | +
|
| 54 | + .. code-block:: php |
| 55 | +
|
| 56 | + // app/config/config.php |
| 57 | + $container->loadFromExtension('framework', array( |
| 58 | + 'trusted_proxies' => array('192.0.0.1', '10.0.0.0/8'), |
| 59 | + )); |
| 60 | +
|
| 61 | +In this example, you're saying that your reverse proxy (or proxies) has |
| 62 | +the IP address ``192.0.0.1`` or matches the range of IP addresses that use |
| 63 | +the CIDR notation ``10.0.0.0/8``. For more details, see :ref:`reference-framework-trusted-proxies`. |
| 64 | + |
| 65 | +That's it! Symfony will now look for the correct ``X-Forwarded-*`` headers |
| 66 | +to get information like the client's IP address, host, port and whether or |
| 67 | +not the request is using HTTPS. |
| 68 | + |
| 69 | +But what if the IP of my Reverse Proxy Changes Constantly! |
| 70 | +---------------------------------------------------------- |
| 71 | + |
| 72 | +Some reverse proxies (like Amazon's Elastic Load Balancers) don't have a |
| 73 | +static IP address or even a range that you can target with the CIDR notation. |
| 74 | +In this case, you'll need to - *very carefully* - trust *all* proxies. |
| 75 | + |
| 76 | +1. Configure your web server(s) to *not* respond to traffic from *any* clients |
| 77 | + other than your load balancers. For AWS, this can be done with `security groups`_. |
| 78 | + |
| 79 | +1. Once you've guaranteed that traffic will only come from your trusted reverse |
| 80 | + proxies, configure Symfony to *always* trust incoming request. This is |
| 81 | + done inside of your front controller:: |
| 82 | + |
| 83 | + // web/app.php |
| 84 | + // ... |
| 85 | + |
| 86 | + Request::setTrustedProxies(array($request->server->get('REMOTE_ADDR'))); |
| 87 | + |
| 88 | + $response = $kernel->handle($request); |
| 89 | + // ... |
| 90 | + |
| 91 | +That's it! It's critical that you prevent traffic from all non-trusted sources. |
| 92 | +If you allow outside traffic, they could "spoof" their true IP address and |
| 93 | +other information. |
| 94 | + |
| 95 | +My Reverse Proxy Uses Non-Standard (not X-Forwarded) Headers |
| 96 | +------------------------------------------------------------ |
| 97 | + |
| 98 | +Most reverse proxies store information on specific ``X-Forwarded-*`` headers. |
| 99 | +But if your reverse proxy uses non-standard header names, you can configure |
| 100 | +these (:doc:`see reference </components/http_foundation/trusting_proxies>`. |
| 101 | +The code for doing this will need to live in your front controller (e.g. ``web/app.php``). |
| 102 | + |
| 103 | +.. _`security groups`: http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/using-elb-security-groups.html |
0 commit comments