Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add a Varnish container and enable cache invalidation#238

Merged
dunglas merged 1 commit intoapi-platform:masterfrom
dunglas:varnish
Jun 6, 2017
Merged

Add a Varnish container and enable cache invalidation#238
dunglas merged 1 commit intoapi-platform:masterfrom
dunglas:varnish

Conversation

@dunglas
Copy link
Member

@dunglas dunglas commented Feb 20, 2017

See api-platform/core#952 for the rationale.

TODO:

  • Disable the cache for app_dev.php

composer.json Outdated
"php": ">=7.0",
"symfony/symfony": "3.2.*",
"api-platform/core": "^2.0",
"api-platform/core": "dev-cache_tags",
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't forget to change it when merging both

import std;

backend default {
.host = "nginx";

Choose a reason for hiding this comment

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

beware, varnish resolves the IP of the domain at boot time and keep it till the end of process.
If the IP of nginx changes (for instance when the container restarts) varnish won't be able to route traffic to the new container

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you know a workaround for this?

Copy link

Choose a reason for hiding this comment

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

Several people use a local nginx configured in proxy pass.
Varnish point to nginx 127.0.0.1, and nginx performs the DNS resolution on the fly.
Some other use a script in charge of reloading varnish when the I'm change.

None is ideal to me.

In dev environment, I just restart my container, in production we use a local nginx

Copy link
Member Author

Choose a reason for hiding this comment

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

I suggest to let it as is for now. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

return(synth(400, "X-Ban-Regex HTTP header must be set."));
}

# Don't cache in dev mode

Choose a reason for hiding this comment

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

Why? Shouldn't be an issue if the invalidator works well.
If it don't, you'll not going on prod.

Copy link
Member Author

@dunglas dunglas Apr 7, 2017

Choose a reason for hiding this comment

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

It hurts the DX. When you change the code in dev, you want the new code to be executed even if the underlying resource has not been modified.

# To allow API Platform to ban by cache tags
if (req.method == "BAN") {
if (client.ip !~ ban) {
return(synth(403, "Not allowed"));

Choose a reason for hiding this comment

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

What's about 405?

# Don't send cache tags related headers to the client
unset resp.http.url;
# Uncomment the following line to NOT send the "Cache-Tags" header to the client (prevent using CloudFlare cache tags)
#unset resp.http.Cache-Tags;

Choose a reason for hiding this comment

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

I wonder if we shouldn't remove the s-max-age header too.
With this new tag feature, the app sets an arbitrary value because we known we'll be able to invalidate the varnish. Which is fine.

BUT what if an other unknown proxy is on the path? (see "proxy cache" in this old version of the documentation http://symfony.com/doc/2.2/book/http_cache.html#types-of-caches)

Copy link
Contributor

Choose a reason for hiding this comment

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

We should use a custom header like X-Reverse-Proxy-TTL instead of abusing s-max-age.

Copy link
Member Author

Choose a reason for hiding this comment

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

For instance, when using CloudFlare, the s-max-age header should be set too. I propose to keep it as is for now too. We'll see when we'll get some feedback.

Copy link
Contributor

Choose a reason for hiding this comment

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

We must not set an incorrect s-max-age. It's 100% dangerous. If the user needs to do that for CloudFlare, they should set it themselves (or we should provide some easy way of setting it).

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't get your point, we don't set this value automatically, it's up to the user. We just provide a good default when using the Varnish image.

Copy link
Contributor

@teohhanhui teohhanhui Jun 7, 2017

Choose a reason for hiding this comment

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

Alright. I see that the default configuration doesn't set maxage and s-maxage, so we're safe. 😄

https://github.com/api-platform/core/blob/9fa028903d1df02011c2b75504b87d1e975e7b70/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php#L123

Copy link
Contributor

Choose a reason for hiding this comment

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

But we provide an unsafe default in api-platform/api-platform. A good default is a safe default. 😞

shared_max_age: 3600

Copy link
Contributor

@teohhanhui teohhanhui Jun 7, 2017

Choose a reason for hiding this comment

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

So my suggestion is that we should use a custom header, e.g. Reverse-Proxy-TTL

Simply because the HTTP cache mechanism is not designed with reverse caching proxies in mind, so it's not the right tool for the job.

Copy link
Member Author

Choose a reason for hiding this comment

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

My concern is that this custom header will not be taken into account by Cloudflare. Maybe can we set the smaxage as we currently do but remove it with Varnish before the request go outside our network?

Copy link
Contributor

Choose a reason for hiding this comment

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

If CloudFlare uses s-maxage, let the user change the shared_max_age option that we already provide in the bundle config. It's not a safe default, and the default setup in api-platform/api-platform uses Varnish anyway, not CloudFlare.

api_platform:
http_cache:
enable_tags: true
varnish_urls: %varnish_urls%

Choose a reason for hiding this comment

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

Quote ?

@dunglas dunglas force-pushed the varnish branch 4 times, most recently from ff6404e to 0c81720 Compare June 2, 2017 18:11
@dunglas
Copy link
Member Author

dunglas commented Jun 2, 2017

Requires api-platform/core#1156

@dunglas dunglas merged commit ddb4e77 into api-platform:master Jun 6, 2017
@dunglas dunglas deleted the varnish branch June 6, 2017 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants