|
| 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 a ``X-Forwarded-For`` header. |
| 13 | + |
| 14 | +If you don't configure Symfony to look for these headers, you'll get incorrect |
| 15 | +information about the client's IP address, whether or not the client is connecting |
| 16 | +via HTTPS, the client's port and the hostname being requested. |
| 17 | + |
| 18 | +Solution: trusted_proxies |
| 19 | +------------------------- |
| 20 | + |
| 21 | +This is no problem, but you *do* need to tell Symfony that this is happening |
| 22 | +and which reverse proxy IP addresses will be doing this type of thing: |
| 23 | + |
| 24 | +.. configuration-block:: |
| 25 | + |
| 26 | + .. code-block:: yaml |
| 27 | +
|
| 28 | + # app/config/config.yml |
| 29 | + # ... |
| 30 | + framework: |
| 31 | + trusted_proxies: [192.0.0.1, 10.0.0.0/8] |
| 32 | +
|
| 33 | + .. code-block:: xml |
| 34 | +
|
| 35 | + <!-- app/config/config.xyml --> |
| 36 | + <framework:config trusted-proxies="192.0.0.1, 10.0.0.0/8"> |
| 37 | + <!-- ... --> |
| 38 | + </framework> |
| 39 | +
|
| 40 | + .. code-block:: php |
| 41 | +
|
| 42 | + // app/config/config.php |
| 43 | + $container->loadFromExtension('framework', array( |
| 44 | + 'trusted_proxies' => array('192.0.0.1', '10.0.0.0/8'), |
| 45 | + )); |
| 46 | +
|
| 47 | +In this example, you're saying that your reverse proxy (or proxies) have |
| 48 | +the IP address ``192.0.0.1`` or match the range of IP addresses that use |
| 49 | +the CIDR notation ``10.0.0.0/8``. For more details, see :ref:`reference-framework-trusted-proxies`. |
| 50 | + |
| 51 | +That's it! Symfony will now look for the correct ``X-Forwarded-*`` headers |
| 52 | +to get information like the client's IP address, host, port and whether or |
| 53 | +not the request is using HTTPS. |
| 54 | + |
| 55 | +But I the IP of my Reverse Proxy Changes Constantly! |
| 56 | +---------------------------------------------------- |
| 57 | + |
| 58 | +Some reverse proxies (like Amazon's Elastic Load Balancers) don't have a |
| 59 | +static IP address or even a range that you can target with the CIDR notation. |
| 60 | +In this case, you'll need to - *very carefully* - trust *all* proxies. |
| 61 | + |
| 62 | +1. Configure your web server(s) to not respond to traffic from *any* servers |
| 63 | + other than your load balancers. For AWS, this can be done with `security groups`_. |
| 64 | + |
| 65 | +1. Once you've guaranteed that traffic will only come from your trusted reverse |
| 66 | + proxies, configure Symfony to *always* trust incoming request. This is |
| 67 | + done inside of your front controller: |
| 68 | + |
| 69 | +.. code-block:: php |
| 70 | +
|
| 71 | + // web/app.php |
| 72 | + // ... |
| 73 | +
|
| 74 | + Request::setTrustedProxies(array($request->server->get('REMOTE_ADDR'))); |
| 75 | +
|
| 76 | + $response = $kernel->handle($request); |
| 77 | + // ... |
| 78 | +
|
| 79 | +That's it! It's critical that you prevent traffic from all non-trusted sources. |
| 80 | +If you allow outside traffic, they could "spoof" their true true IP address |
| 81 | +and other information. |
| 82 | + |
| 83 | +My Reverse Proxy Uses Non-Standard (not X-Forwarded-*) Headers |
| 84 | +-------------------------------------------------------------- |
| 85 | +
|
| 86 | +Most reverse proxies store information on specific ``X-Forwarded-*`` headers. |
| 87 | +But if your reverse proxy uses non-standard header names, you can configure |
| 88 | +these. See :doc:`/components/http_foundation/trusting_proxies`. The code |
| 89 | +for doing this will need to live in your front controller (e.g. ``web/app.php``). |
| 90 | + |
| 91 | +.. _`security groups`: http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/using-elb-security-groups.html |
0 commit comments