-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Adding a new entry about reverse proxies in the framework #4102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
18af4e8
c55bc2e
81053ab
89e4d9d
5ab6c4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,6 +162,11 @@ kernel:: | |
The caching kernel will immediately act as a reverse proxy - caching responses | ||
from your application and returning them to the client. | ||
|
||
Now that you're using a "proxy", you'll need to configure ``127.0.0.1`` under | ||
the ``trusted_proxies`` configuration. See | ||
:ref:`framework.trusted_proxies <reference-framework-trusted-proxies>`. Without | ||
this, the client's IP address and a few other things won't report correctly. | ||
|
||
.. tip:: | ||
|
||
The cache kernel has a special ``getLog()`` method that returns a string | ||
|
@@ -1005,8 +1010,8 @@ possible. | |
|
||
.. tip:: | ||
|
||
The listener only responds to local IP addresses or trusted | ||
proxies. | ||
The listener only responds to local IP addresses or | ||
:doc:`trusted proxies</cookbook/request/load_balancer_reverse_proxy>`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space between proxies and the opening angle bracket |
||
|
||
.. note:: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,11 @@ | |
Trusting Proxies | ||
================ | ||
|
||
.. tip:: | ||
|
||
If you're using the Symfony Framework, start by reading | ||
:doc:`/cookbookrequest/load_balancer_reverse_proxy`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing slash between cookbook and request. |
||
|
||
If you find yourself behind some sort of proxy - like a load balancer - then | ||
certain header information may be sent to you using special ``X-Forwarded-*`` | ||
headers. For example, the ``Host`` HTTP header is usually used to return | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,13 @@ Because Symfony2's cache uses the standard HTTP cache headers, the | |
proxy. `Varnish`_ is a powerful, open-source, HTTP accelerator capable of serving | ||
cached content quickly and including support for :ref:`Edge Side Includes <edge-side-includes>`. | ||
|
||
Trusting Reverse Proxies | ||
------------------------ | ||
|
||
For ESI to work correctly and for the :ref:`X-FORWARDED<varnish-x-forwarded-headers>` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. musing space before the opening angle bracket |
||
headers to be used, you need to configure Varnish as a | ||
:doc:`trusted proxy</cookbook/request/load_balancer_reverse_proxy>`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
|
||
.. index:: | ||
single: Varnish; configuration | ||
|
||
|
@@ -188,6 +195,8 @@ that will invalidate the cache for a given resource: | |
} | ||
} | ||
|
||
.. _varnish-x-forwarded-headers: | ||
|
||
Routing and X-FORWARDED Headers | ||
------------------------------- | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ Request | |
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
load_balancer_reverse_proxy | ||
mime_type |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
How to Configure Symfony to Work behind a Load Balancer or Reverse Proxy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [...] or a Reverse Proxy |
||
======================================================================== | ||
|
||
When you deploy your application, you may be behind a load balancer (e.g. | ||
an AWS Elastic Load Balancer) or a reverse proxy (e.g. Varnish for | ||
:doc:`caching</book/http_cache>`). | ||
|
||
For the most part, this doesn't cause any problems with Symfony. But, when | ||
a request passes through a proxy, certain request information is sent using | ||
special ``X-Forwarded-*`` headers. For example, instead of reading the ``REMOTE_ADDR`` | ||
header (which will now be the IP address of your reverse proxy), the user's | ||
true IP will be stored in a ``X-Forwarded-For`` header. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in an |
||
|
||
.. tip:: | ||
|
||
If your using Symfonys :ref:`AppCache<symfony-gateway-cache>` for caching, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and "you're" |
||
then you *are* using a reverse proxy with the IP address ``127.0.0.1``. | ||
You'll need to configure that address as a trusted proxy below. | ||
|
||
If you don't configure Symfony to look for these headers, you'll get incorrect | ||
information about the client's IP address, whether or not the client is connecting | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get the need to add this phrase here: "whether or not the client is connecting via HTTPS". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I get it now: the "whether is HTTPS or not" phrase is not related to the previous phrase about the IP address. |
||
via HTTPS, the client's port and the hostname being requested. | ||
|
||
Solution: trusted_proxies | ||
------------------------- | ||
|
||
This is no problem, but you *do* need to tell Symfony that this is happening | ||
and which reverse proxy IP addresses will be doing this type of thing: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/config.yml | ||
# ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. standards say that there should be an empty line before this line |
||
framework: | ||
trusted_proxies: [192.0.0.1, 10.0.0.0/8] | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xyml --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. xml |
||
<framework:config trusted-proxies="192.0.0.1, 10.0.0.0/8"> | ||
<!-- ... --> | ||
</framework> | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like to have the XML complete (meaning to have all elements present that sorround the |
||
.. code-block:: php | ||
|
||
// app/config/config.php | ||
$container->loadFromExtension('framework', array( | ||
'trusted_proxies' => array('192.0.0.1', '10.0.0.0/8'), | ||
)); | ||
|
||
In this example, you're saying that your reverse proxy (or proxies) have | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. has |
||
the IP address ``192.0.0.1`` or match the range of IP addresses that use | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. matches |
||
the CIDR notation ``10.0.0.0/8``. For more details, see :ref:`reference-framework-trusted-proxies`. | ||
|
||
That's it! Symfony will now look for the correct ``X-Forwarded-*`` headers | ||
to get information like the client's IP address, host, port and whether or | ||
not the request is using HTTPS. | ||
|
||
But I the IP of my Reverse Proxy Changes Constantly! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
---------------------------------------------------- | ||
|
||
Some reverse proxies (like Amazon's Elastic Load Balancers) don't have a | ||
static IP address or even a range that you can target with the CIDR notation. | ||
In this case, you'll need to - *very carefully* - trust *all* proxies. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change all proxies by any proxy in front of your web servers here all seems like a very strong word :) |
||
|
||
1. Configure your web server(s) to not respond to traffic from *any* servers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't it be "[...] not to respond to [...]"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xabbuh both work - but the way I did it puts emphasis on the "not" part. But I just added some There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
other than your load balancers. For AWS, this can be done with `security groups`_. | ||
|
||
1. Once you've guaranteed that traffic will only come from your trusted reverse | ||
proxies, configure Symfony to *always* trust incoming request. This is | ||
done inside of your front controller: | ||
|
||
.. code-block:: php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll try it - I wasn't sure if the previous being a bullet would cause any issues |
||
|
||
// web/app.php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this complete code block should be indented with 3 more spaces (3 spaces is now the new outline base in the list, and code blocks need to be indented with 4 spaces so 3 + 4 = 7 spaces, you use 4 spaces now) |
||
// ... | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line should be moved above the previous line |
||
Request::setTrustedProxies(array($request->server->get('REMOTE_ADDR'))); | ||
|
||
$response = $kernel->handle($request); | ||
// ... | ||
|
||
That's it! It's critical that you prevent traffic from all non-trusted sources. | ||
If you allow outside traffic, they could "spoof" their true true IP address | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. duplicated |
||
and other information. | ||
|
||
My Reverse Proxy Uses Non-Standard (not X-Forwarded) Headers | ||
------------------------------------------------------------ | ||
|
||
Most reverse proxies store information on specific ``X-Forwarded-*`` headers. | ||
But if your reverse proxy uses non-standard header names, you can configure | ||
these. See :doc:`/components/http_foundation/trusting_proxies`. The code | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would put the "see reference" in parentheses. |
||
for doing this will need to live in your front controller (e.g. ``web/app.php``). | ||
|
||
.. _`security groups`: http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/using-elb-security-groups.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would put the "see reference" onto parentheses.