Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/frameworks/symfony.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ Since [the filesystem is readonly](/docs/environment/storage.md) except for `/tm
}
```

An optional last step is to set "Trusted proxy" and "Trusted hosts". Failing to add
these will cause issues like:
- your application don't generate links using https.
- impossible to capture user IP address
- failure to recognize host names

That could be done with [environment variables](#Environment variables) or by running
these lines in `public/index.php`.

```php
use Symfony\Component\HttpFoundation\Request;

Request::setTrustedProxies(['127.0.0.1'], Request::HEADER_X_FORWARDED_ALL);
Request::setTrustedHosts(['api\.example\.com']);
```

## Deploy

Your application is now ready to be deployed. Follow [the deployment guide](/docs/deploy.md).
Expand Down Expand Up @@ -127,8 +143,28 @@ Since Symfony 4, the production parameters are configured through environment va
provider:
environment:
APP_ENV: prod
TRUSTED_PROXIES: '127.0.0.1'
TRUSTED_HOSTS: '^api\.example\.com$'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these environment variables used? I can't see them in the diff.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I think I understand from the diff above:

That could be done with [environment variables](#Environment variables) or by running
these lines in public/index.php.

Simply setting those variables is enough? I would say that it's better to document one solution, it is simpler and less confusing. The env var seem quite simple to setup (compared to editing Symfony code), what do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the link, turns out we commented at the same time.

What do you think about recommending only the environment variables? (for simplicity)

```

The secrets (e.g. database passwords) must however not be committed in this file.

To learn more about all this, read the [environment variables documentation](/docs/environment/variables.md).

## Getting the user's IP

If your application needs the user's IP you must fetch it from the `LAMBDA_CONTEXT` variable.
Modify `public/index.php` accordingly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to forward those values automatically in Bref itself?

I.e. maybe (not sure) set a header with the value that is in the lambda context?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of these solutions were discussed in #501

However, I do think it makes sense to cherry-pick useful variables. Ie creating LAMBDA_CONTEXT_IDENTITY_SOURCEIP

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but beyond #501 (which is about the request context containing many things), I'm focusing especially on HTTP-related stuff here: if we have the clients' IP address, we could maybe "fix" the HTTP headers (in the FPM runtime only) so that they contain the correct values out of the box?


```diff

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is diff supported?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so yes.

+// Get user IP:
+if (isset($_SERVER['LAMBDA_CONTEXT'])) {
+ $context = json_decode($_SERVER['LAMBDA_CONTEXT'], true);
+ $_SERVER['HTTP_X_FORWARDED_FOR'] = $context['identity']['sourceIp'] ?? '';
+}

$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$request = Request::createFromGlobals();
```

This will only work if you configured "Trusted proxies" properly.