How to configure symfony 1.4 for use with varnish?

Yesterday I had a huge load on my servers, and even if I worked on optimizing performance earlier (I had a similar problem about 2 months ago), my servers could not cope with the load (I have a service that had about 50 creations in minute).

Finally, my servers handled the load because I changed the instances: I am on Amazon EC2 and I used a load balancer with 20 micro instances. This was not enough. Finally, I changed to 10 large copies, and everything was fine. But, you know, large instances of the road, and I can not afford to have so many large instances (now, since there is less load, I only have "5 large instances, but this is too much).

So, I'm still working on optimizing and configuring the server, but I'm stuck on a point.

So far I am using symfony with memcached. It works great, everything you need to cache is cached, etc.

Now I want to add varnish in front of the apache web server.

I did it and I set it up - I think ... well, and it works now. The problem is that there is no hit in the cache.

From what I saw, the problem is that the HTTP headers sent by symfony are not set correctly. For example, for a cached request, I have the following headers:

Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache 

The module is configured correctly to use cache, etc., but I just can't find where I can set the correct HTTP headers. I know how to set the cache header in symfony for a specific action, but I'm sure I donโ€™t want to do this with every action (by the way, even if I did this, I think this is the wrong way).

So, I ask how I can use Varnish with symfony 1.4. From what I saw, there are two possibilities:

  • I set the HTTP headers sent by symfony correctly
  • I configure Varnish to correctly handle default HTTP headers sent by symfony

Do you know how I can fix one of the problems?

Thanks,

NB: I'm on Varnish3

+4
source share
1 answer

I finally found for myself how to solve my problem, so Iโ€™ll tell you how I did it:

To get started, you need to know that symfony automatically creates a PHP session every time a page is called. So, I did this to disable this default behavior. To do this, I added a factory repository (default: sfSessionStorage) to the .yml factories with the auto_start parameter set to false :

 storage: class: sfSessionStorage param: auto_start: false 

Then I created a filter to handle http headers:

First I added it to the filters.yml file

 http_header: class: myHttpHeaderFilter 

And then I added the myHttpHeaderFilter.php class to the lib folder, where I processed all the headers I wanted. For instance:

 class myHttpHeaderFilter extends sfFilter { public function execute($filterChain) { //execute the next filter in the chain $filterChain->execute(); //code after here runs after action // Filters don't have direct access to the request and user objects. // You will need to use the context object to get them $request = $this->getContext()->getRequest(); $path = $request->getPathInfo(); if (substr($path, 0, 5) == "/foo") // We cache only some of the /foo requests { if (strstr($path, "bar")) // We cache the request containing bar during half an hour hour $this->getContext()->getResponse()->addCacheControlHttpHeader('max-age=1800'); else // All the other requests are cached during 24 hours { $this->getContext()->getResponse()->addCacheControlHttpHeader('max-age=86400'); } } else // The other requests are not cached $this->getContext()->getResponse()->addCacheControlHttpHeader('no-cache, no-store'); } } 

And it's all!

I also modified vcl_recv on the server side to ensure that all requests that do not need to be cached are not (theoretically, this is not necessary, because I processed it on symfony, it's just a "crosscheck").

 sub vcl_recv { if (req.http.Authorization || req.http.Cookie) { /* Not cacheable by default */ return (pass); } if (req.request != "GET" && req.request != "HEAD") { /* We only deal with GET and HEAD by default */ return (pass); } if (req.url ~ "/user") /* Requests containing user data are never cached */ { return (pass); } return (lookup); } 
+6
source

Source: https://habr.com/ru/post/1382552/


All Articles