简体   繁体   English

强制Codeigniter使用清漆

[英]Forcing Codeigniter to work with varnish

I have a codeiginter application that already use codeiginter build-in caching, I applied Varnish as a new caching layer. 我有一个已经使用codeiginter内置缓存的codeiginter应用程序,我将Varnish用作新的缓存层。 But as i can see from varnishstat it's not making that difference. 但是,正如我从varnishstat中看到的那样,它没有什么区别。

Hitrate ratio:        1        1        1
Hitrate avg:     0.0480   0.0480   0.0480

I think thats because of Codeiginter cookies and HTTP Headers that are being sent back. 我认为那是因为Codeiginter Cookie和HTTP标头被发送回了。

This is the http request coming from varnish. 这是来自清漆的http请求。

Accept-Ranges:bytes
Age:0
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Content-Encoding:gzip
Content-Length:1755
Content-Type:text/html
Date:Mon, 29 Jul 2013 07:25:59 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Pragma:no-cache
Server:Apache/2.2.22 (Ubuntu)
Set-Cookie:ci=%2BEZFRauewyNnl3CXHE0cmz6F3G3moadilt5vXu5gghKXkWm4gg0JObnF2Etq6J5rl3XVigbF0je3funibpvEi4X%2BT3rS2VmazGG3T4Vm1%2B8YcbqRgL4xuMcxybndqJy%2BU9mNmsJjOgmYEZ8oPG8BKOtMaxNiMHmgmbBydxt3SzKfFfFUOydFx%2BeeJ7P1WE7V10m6GyfnFO5DrFYNsE%2F37WNAI%2Bpux%2Fwwch5B4DH1%2F6wssPm%2BNwsLQ1%2FBd44hgUXe3mMzzcqvxNjKqs0gjuwzwPT4nibEHirfaJ7TMVGObMjdrbREnoYS2gwoN15cCeKgXmTJQI2vvTuPcdtZVCjcAX6OvTy491HdIvQIdKRhX2BNi8d7ygo%2F7n5T6%2FN%2B0IohNN9iZ%2Fh959W%2Fz4azEJPfTrluucf6cLnlp2T2zb%2Fb3XroWuPqguk4wMpsAstfLsSfA%2F6yEi4Hph%2BPFxX%2BhyBazs11LJ38FA0flWtYY%2Bk%2B6yoF13sTaENN2pWj0bKDTtres9E4y3xMPr%2FZaO78WRA9CccDzcQfbZ3bZUqoXg4HmX%2BHDHiYPLD6uFpnC28LuDrCSbgXFIlhDrC8j65sxNSKhnzlUP7Konr%2FKRfKNzrgtWHBEzuXArW%2BlgIg1MzaW3GIkRy1gr16ZUjIiv7CCx7Y2twAfKOm4t00MvrTcFoxBPN1lzoasNyRLMIvshU8heWZHy17OPEapuO6N%2BuMl9L8LqU0%2FF%2BUeUDyFVwLG39LGkIVuF93VsIYEp6w2UwtccX4OO4P2uwJEoAJMMqUE%2FztELpCv%2BkfRAiub48n%2BRxK%2FhgUHw1LWsWIPv3xngq3MI8ypWCqkWLjPuu5dc%2FdOd3BSW2MYcBwacoB5CEOPBHGq3hw1QSZfY330hkLuyQPHxkh%2FDTija%2FN2Rz6z47JorsCqHGDBK6%2BPswBWvYZeMd0VMD%2F95j%2BFibi6rBqL3hoE%2BDgcfCdly%2FYH9py%2Fe%2Fa0AUiIINTK8EPtpuKdC8dLhKo2jI5J4e1ifZuWjVd3VnL2CvX; path=/
Vary:Accept-Encoding
Via:1.1 varnish
X-Powered-By:PHP/5.3.10-1ubuntu3.5
X-Varnish:1353481467

I noticed difference things: 我注意到了一些不同之处:

  1. No matter how hard i try codeiginter won't change the Cache-Control , Age , Expires until i set them manually 无论我多么努力尝试codeiginter都不会更改Cache-ControlAgeExpires直到我手动设置它们
  2. Codeiginiter Sent an old date 1981 I doubled checked the date on my server it's correct Codeiginiter发送了一个旧日期1981我仔细检查了服务器上的日期是否正确
  3. Codeiginiter keep changing the cookies almost each request. Codeiginiter几乎在每个请求中都会更改cookie。

In my app i have several pages that require username/password but i'm trying to focus first on getting the public pages to be cached. 在我的应用程序中,我有几个需要用户名/密码的页面,但我试图首先集中精力获取要缓存的公共页面。 after that I will check the account related pages. 之后,我将检查与帐户相关的页面。

I'm testing on this code: 我正在测试此代码:

//$this->output->set_header("Cache-Control:public, max-age=9000");
//$this->output->set_header("Vary: Accept-Encoding,Cookie,User-Agent");  
$this->output->cache(2400);
$this->load->view("test");

If your backend is sending Cache-control: max-age=XXX you can forget about Expires header, as it will be ignored in HTTP 1.1 compliant clients & proxys [1] (In fact, it's usual to set it on the past in order to avoid old HTTP 1.0 clients caching items). 如果您的后端发送的是Cache-control: max-age=XXX您可以忽略Expires标头,因为它会在与HTTP 1.1兼容的客户端和代理中被忽略[1](实际上,通常按顺序将其设置为过去)以避免旧的HTTP 1.0客户端缓存项目)。

Be very careful when setting Vary: User-Agent header, as it can harm your caching chances [2] 设置Vary: User-Agent标头时要非常小心,因为它可能会损害您的缓存机会[2]

And finally, Varnish won't cache a request such as that because of the Set-Cookie header. 最后,由于Set-Cookie标头,Varnish不会缓存这样的请求。 Varnish needs lazy session initializing (see [3]) to cache thing (or a quite complicated VCL) Varnish需要惰性会话初始化(请参阅[3])来缓存事物(或相当复杂的VCL)

If you fix the cookie thing and still have problems the VCL file and varnish version will be really apreciated. 如果您修复了cookie的问题,但仍然有问题,那么VCL文件和清漆的版本将非常有用。

[1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html [1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

[2] https://www.varnish-cache.org/docs/3.0/tutorial/vary.html [2] https://www.varnish-cache.org/docs/3.0/tutorial/vary.html

[3] How should I implement lazy session creation in PHP? [3] 如何在PHP中实现延迟会话创建?

Your cookie problem is one I'm currently wrestling with. 您的Cookie问题是我目前正在努力解决的问题。 I'm guessing from your sample that you have changed the cookie_name variable to be ci rather than ci_session. 我从您的样本中猜测,您已将cookie_name变量更改为ci而不是ci_session。

The simplest thing for you - for certain values of simple - may be to jettison CI's session handling for an alternative or switch to db-based cookies rather than cookie-based. 对于您来说,最简单的事情-对于某些简单值-可能是放弃CI的会话处理以寻求替代方案,或者切换到基于db的cookie,而不是基于cookie的cookie。 If there's a set-cookie - and there always is when you have sessions initialized/auto-loaded - it won't get cached. 如果有一个set-cookie,并且在您初始化/自动加载会话时总是存在-则不会缓存它。 If the data is all stored server-side instead you won't have this issue. 如果数据全部存储在服务器端,则不会出现此问题。

Personally I'm not gung-ho to introduce databases for our use of sessions so I'm going to experiment with setting the cache-control header to ignore cookies and using header_remove() at the top of views I know will never be user-specific (like our RSS feed). 我个人不是为了介绍会话使用而介绍数据库,因此我将尝试设置缓存控制标头以忽略cookie,并在视图顶部使用header_remove(),我知道它将永远不会成为用户,具体(例如我们的RSS feed)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM