除非更改后端TTL,否则Varnish缓存不使用会话缓存PHP [英] Varnish Cache not Caching PHP with Sessions Unless backend TTL altered

查看:117
本文介绍了除非更改后端TTL,否则Varnish缓存不使用会话缓存PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Varnish Cache的新手,我希望有一个问题可以帮助我。

I'm new to Varnish Cache and have a question I hope I can get some help with.

我有一个非常简单且基本的设置,但没有用据我了解
应该出于某种原因。

I have a very simple and basic setup but it's not working as I understand it should for some reason.

这与Varnish没有缓存使用cookie的PHP页面有关。

It's related to Varnish not caching PHP pages that are using cookies.

这是我的设置:

1)对于我的default.vcl,我有一个简单的后端

1) For my default.vcl I have a simple backend

backend default {
.host = "127.0.0.1";
.port = "80";
}

2)我有一个简单的PHP文件,只有以下两行: / p>

2) I have a simple PHP file that has only these two line:

session_start();
echo time();

3)
当我调用此页面时,它不会正确缓存
作为我没有添加必需的vcl规则

3) When I call this page it correctly does not cache as I have not added in the required vcl rules

4)

因此,根据我对文档的理解,在这两个规则中添加

So as per my understanding of the documentation I add in these two rules

sub vcl_recv {
unset req.http.Cookie;
return (lookup);
}

sub vcl_fetch {
unset beresp.http.Set-Cookie;
return(deliver);
}

5)
PHP页面仍然不会缓存。
我可以看到 Set-Cookie 标头已被删除
,因为我在FireFox中使用FireBug。

5) The PHP page still will not cache. I can see the Set-Cookie header has been removed as I'm using FireBug in FireFox.

只有将其添加到sub vcl_fetch
中,PHP才会缓存:

It's only if I add this to sub vcl_fetch that the PHP will cache:

set beresp.ttl = 24h;

我的问题是否正确?

我认为我不需要更改后端响应的ttl
。我以为只是取消输入和退出
cookie就会迫使带有会话的PHP缓存。

I didn't think I would need to alter the ttl of the backend response. I thought just unsetting cookies in and out would force PHP w/ session to cache.

我完整的默认vcl是:

My complete default vcl is:

backend default {
.host = "127.0.0.1";
.port = "80";
}

sub vcl_recv {
unset req.http.Cookie;
return (lookup);
}

sub vcl_fetch {
unset beresp.http.Set-Cookie;
set beresp.ttl = 24h;
return(deliver);
}

我的启动命令是:

varnishd -f /etc/varnish/default.vcl -s malloc,128M -T 127.0.0.1:2000 -a 0.0.0.0:8080

我正在呼叫的网址是:

http://vbox.local:8080/varnish-tests/index.php

我的index.php文件只有:

My index.php file has only:

<?php 
session_start();
echo time();

我想问社区,这看起来是否正确,或者我错了。本质上,我只是不确定为什么要有
来添加beresp.ttl = 24h来最终使页面缓存
上清漆。

I would like to ask the community if this looks correct or if I am wrong. Essentially I'm just unsure why I had to add the beresp.ttl = 24h to finally have the page cache in varnish.

I认为我不需要这个。

I thought I would not need this.

任何建议都值得赞赏。

Any advise much appreciated.

谢谢!

亲切的问候。

推荐答案

Varnish将服从响应中的缓存标头。 PHP将发送缓存控制标头,而不按默认值

Varnish will obey the caching headers on the response. PHP will send cache-control headers NOT to cache the response by deafult

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma:no-cache

您需要禁用这些标头,否则Varnish将服从并因此不缓存页面。要关闭它们,只需使用空字符串调用session_cache_limiter()

You need to disable those headers otherwise Varnish will obey and thus not cache the page. To turn them off simply call session_cache_limiter() with an empty string

session_cache_limiter('');
header("Cache-Control: public, s-maxage=60");
session_start();

然后可以添加标头,将缓存控件设置为public。使用上面的三行将启用缓存。

You can then add a header to set the cache-control to public. Using the three lines above will enable caching.

这篇关于除非更改后端TTL,否则Varnish缓存不使用会话缓存PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆