Skip to content

Inconsistent response headers when using caching via Cache attribute #58598

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

Open
EugeniyPetrov opened this issue Oct 18, 2024 · 4 comments
Open

Comments

@EugeniyPetrov
Copy link

Symfony version(s) affected

7.1.5

Description

When HTTP caching is set using #[Cache] attribute, and only if HTTP/1.0 (which is the default protocol nginx uses to request upstream when configured as reverse proxy) is used, symfony adds headers that prevent response from being cached.

How to reproduce

#[Route('/test1', name: 'test1')]
public function test1(): Response
{
    return (new Response("Hello\n"))
        ->setPublic()
        ->setMaxAge(31536000);
}

#[Route('/test2', name: 'test2')]
#[Cache(maxage: 31536000, public: true)]
public function test2(): Response
{
    return new Response("Hello\n");
}
$ curl --http1.0 -i 'http://127.0.0.1:8002/test1'
HTTP/1.0 200 OK
Cache-Control: max-age=31536000, public
Content-Type: text/html; charset=UTF-8
Date: Fri, 18 Oct 2024 10:37:32 GMT
X-Powered-By: PHP/8.3.10
Content-Length: 6

Hello

$ curl --http1.0 -i 'http://127.0.0.1:8002/test2'
HTTP/1.0 200 OK
Cache-Control: max-age=31536000, public
Content-Type: text/html; charset=UTF-8
Date: Fri, 18 Oct 2024 10:37:33 GMT
Expires: -1                                         <-- prevents caching
Pragma: no-cache                                    <--
X-Powered-By: PHP/8.3.10
Content-Length: 6

Hello

Possible Solution

No response

Additional Context

No response

@MatTheCat
Copy link
Contributor

Indeed in that case the response will be set private and

$headers->set('pragma', 'no-cache');
$headers->set('expires', -1);
will run before CacheAttributeListener::onKernelResponse does.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Friendly ping? Should this still be open? I will close if I don't hear anything.

@EugeniyPetrov
Copy link
Author

I confirm that the issue is still present as of version 7.2.6.

@carsonbot carsonbot removed the Stalled label May 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants