question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cache header never change

See original GitHub issue

Hello Team,

I change all type max-age in config, entity but always send max-age = 0 , s-maxage= 3600 but i wan’t change it to

http_cache:
        invalidation:
            enabled: true
            varnish_urls: [ '%env(VARNISH_URL)%' ]
        max_age: 3600
        shared_max_age: 3600
        vary: [ 'Content-Type', 'Authorization', 'Origin', 'Accept' ]
        public: true

i can set bresp.http.cache-header in varnish.vcl manually and it’s work but it’s static way i want dynamic way to configure caching system per each entity or global side

here is my varnish.vcl

vcl 4.0;

import std;

backend default {
  .host = "caddy";
  .port = "80";
  # Health check
  #.probe = {
  #  .url = "/";
  #  .timeout = 5s;
  #  .interval = 10s;
  #  .window = 5;
  #  .threshold = 3;
  #}
}

# Hosts allowed to send BAN requests
acl invalidators {
  "localhost";
  "php";
}

sub vcl_recv {
  if (req.restarts > 0) {
    set req.hash_always_miss = true;
  }

  # Remove the "Forwarded" HTTP header if exists (security)
  unset req.http.forwarded;
  # Remove "Preload" and "Fields" HTTP header to improve Vulcain's performance
  unset req.http.preload;
  unset req.http.fields;

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

    if (req.http.ApiPlatform-Ban-Regex) {
      ban("obj.http.Cache-Tags ~ " + req.http.ApiPlatform-Ban-Regex);

      return (synth(200, "Ban added"));
    }

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

  # For health checks
  if (req.method == "GET" && req.url == "/healthz") {
    return (synth(200, "OK"));
  }
}

sub vcl_hit {
  if (obj.ttl >= 0s) {
    # A pure unadulterated hit, deliver it
    return (deliver);
  }

  if (std.healthy(req.backend_hint)) {
    # The backend is healthy
    # Fetch the object from the backend
    return (restart);
  }

  # No fresh object and the backend is not healthy
  if (obj.ttl + obj.grace > 0s) {
    # Deliver graced object
    # Automatically triggers a background fetch
    return (deliver);
  }

  # No valid object to deliver
  # No healthy backend to handle request
  # Return error
  return (synth(503, "API is down"));
}

sub vcl_deliver {
  # Don't send cache tags related headers to the client
  unset resp.http.url;
  # Comment the following line to send the "Cache-Tags" header to the client (e.g. to use CloudFlare cache tags)
  unset resp.http.Cache-Tags;
}

sub vcl_backend_response {
 # set beresp.http.cache-control = "public, max-age=3600";
 set beresp.ttl = 24h;
 set beresp.grace = 1h;
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
arjanfranscommented, Oct 6, 2021

Take a look at the sylius/vendor/symfony/http-kernel/EventListener/AbstractSessionListener.php class. It automatically overrides the cache headers if there is a session. You can disable it by creating a custom response listener that sets the header to disable it.

 * Sets the session onto the request on the "kernel.request" event and saves
 * it on the "kernel.response" event.
 *
 * In addition, if the session has been started it overrides the Cache-Control
 * header in such a way that all caching is disabled in that case.
 * If you have a scenario where caching responses with session information in
 * them makes sense, you can disable this behaviour by setting the header
 * AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER on the response.

I think this should be documented by API Platform though, I spent a few hours before I figured out about this.

The question is, why does it even override it, if the API is stateless?

0reactions
stijnveekecommented, Jul 15, 2022

Have you tried this on different URLs? I got the same problem and realized that this is because I exceeded the request size of Varnish, but the only error I get is a 503 when this happens, mentioning that I’m missing a specific header needed to access the URL because of cors. But, of course, this error occurs in Varnish and not in Symfony Varnish doesn’t send the supplied headers, causing a situation where the HTTP headers would never change.

I would really like some documentation of this because I spend hours working on this problem, and I think at least a user should be informed when using the API platform about this situation to prevent it because Varnish is used as default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cache-Control - HTTP - MDN Web Docs
The Cache-Control HTTP header field holds directives (instructions) ... max-age value and immutable because the content will never change.
Read more >
What is Cache-Control and How HTTP Cache Headers Work
Cache -control is an HTTP header used to specify browser caching policies in both client requests and server responses. Policies include how a...
Read more >
Caching Header Best Practices - Simon Hearne
Caching headers are surprisingly complex and often misconfigured. ... can be stored by the browser indefinitely because they never change.
Read more >
What's the Best Way to Set The Cache-Control Header?
If the page is changed frequently, like the list page for the e-commerce website. Then we should let the user check if there...
Read more >
Caching headers: A practical guide for frontend developers
Including the Pragma: no-cache header is a precaution that protects legacy servers that don't support newer cache control mechanisms and could ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found