Recent posts

Advanced mail subscription

The CMS TYPO3 extension repository already offers a few plugins that can...
09 January 2012

Convenient In/Out animation using expressions

We recently had an opportunity to make a set of instructional animations...
16 December 2012

Nginx boost extension

Introduction  The Evo_nginx_boost plugin allows us to cache pages...
14 December 2011

Tags

How to boost/speed up your TYPO3 website with nginx.

NOTICE. New version available. Now you can use evo_nginx_boost without nginx. You need ony TYPO3 and memcache. New manual of evo_nginx_boost - techblog.evo.pl/en/evo_nginx_boost-extension/

TYPO3 cache

The TYPO3 performance issue is often a subject of discussion. I found a number of various tricks, which can speed up the page generation time, including nc_staticfilecache and dmc_highperformance modules. The latest alpha version of TYPO3 contains radical changes in the caching mechanism, enabling us to choose from various support functions, such as file, memcache, database or apc.

Problem

All of the above solutions, with the exception of nc_staticfilecache, have one thing in common: in order to read cache, one has to invoke a php process, read and parse the cached data, and then send the information to the browser.

Let’s perform a simple test using the ab tool. We will check the number of queries per second for a complex Web

zaznacz
  1.  
  2. ab -n 100 -c 1 <a href="http://localhost/" >localhost</a>
  3. Server Software: Apache/2.2.6
  4. Server Hostname: localhost
  5. Server Port: 80
  6. Total transferred: 5064709 bytes
  7. HTML transferred: 5031914 bytes
  8. Requests per second: 1.02 [#/sec] (mean)
  9. Time per request: 976.265 [ms] (mean)
  10. Time per request: 976.265 [ms] (mean, across all concurrent requests)
  11. Transfer rate: 50.66 [Kbytes/sec] received

The number of request per second amounts to 1.02! 

What if our site has to support a thousand of such queries every second? Even with the most efficient caching mechanism, each PHP parser call will result in very heavy server load.

Nginx

The NGINX proxy server can help us with this problem. While not a completely new solution, a clever combination of this proxy and TYPO3 can result in a two hundred-fold performance increase! The solution I’m describing will support traffic for logged in and guest users, can take into account any Web site changes implemented upon user login, as well as support various TYPO3 cache settings.

This is how it looks on diagram:

Nginx Typo3 and Memcache

 

The user requests a page. Nginx checks the user’s cookie. If we set a cookie named nginx_boost_noCache, the query is sent directly to the Apache server and then to TYPO3, bypassing the memchache entirely. Thus, the cookie allows for easy control of memcache caching mechanism, providing the ability to turn the mechanism off unconditionally in specific situations. This can be implemented in any plugin present in our system. An excerpt from the nginx config file used to support this situation is presented below.

zaznacz
  1.  
  2. if ($http_cookie ~* "nginx_boost_noCache=1") {
  3. proxy_pass <a href="http://domain" >domain</a>; break;
  4. }

If nginx cannot find the nginx_boost_noCache cookie, it will check for a cached page in memcache. $request_uri is used as a search key.

zaznacz
  1.  
  2. # set default memcache key
  3. set $memcached_key $request_uri;
  4. # Check if local memcached server can answer this request
  5. default_type text/html;
  6. memcached_pass 192.168.168.3:11211;

If our page is not present in the memcache, nginx will again send a query to the Apache server. Apache, in turn, invokes TYPO3, generates the page, and evo_nginx_boost writes this page to the memcache, using the requested url for the memcache_key.

$memcache_key = "domain/about-us"

Each subseqent request for this page will be sent directly from the memcache, without the need to run the PHP process again.

Our plug-in fully supports TYPO3 caching times for each party, as well as the cache_period value. If the caching time value is not set in TYPO3, we will be using a default value set in the plug-in’s configuration file.

What if the user logs in?

It all depends on what we do. Using nginx and evo_nxginx_boost, we can completely disable caching for logged in users.  However, while analysing our needs and web page statistics, we have come to the conclusion that logged in users often read - and return to - the same pages. A change must be made when the user sends a POST request, i.e.: writes a comment or a blog article. In this situation, will be using the hook in the class.tslib_fe.php – initFEuser class.

If the user logs in, we set the nginx_boost_fe_user cookie to the value of the fe_typo_user cookie.

$memcache_key = "domain/blogs/us1b27c2c23bdb600912561e08a7e4886b"

Upon receiving a POST or another operation which requires content refresh, we add versions to this value. The evon_nginx_boost plugin then writes a new version of the page to the memcache. 

$memcache_key = "domain/blogs/us1b27c2c23bdb600912561e08a7e4886b_1"

After a five minute period, this version of the page will expire and nginx will send a new request to Apache and Typo3. If another user adds a comment to the blog at this time, we will see it in 5 minutes.

It would also be advisable to set short caching times to pages with quickly-chanigng content. In a social website under a heavy load, we remove the page from cache in 1 to 5 minutes, which gives us the ability to support heavy traffic.

Now to perform ab tests of the above solution.

zaznacz
  1.  
  2. ab -n 100 -c 1 <a href="http://localhost/" >localhost</a>
  3. Server Software: nginx/0.6.35
  4. Server Hostname: localhost
  5. Server Port: 80
  6. Total transferred: 10103800 bytes
  7. HTML transferred: 10089200 bytes
  8. Requests per second: 400.05 [#/sec] (mean)
  9. Time per request: 2.500 [ms] (mean)
  10. Time per request: 2.500 [ms] (mean, across all concurrent requests)
  11. Transfer rate: 39468.58 [Kbytes/sec] received

The number of request per second is 400.05!

evo_nginx_boost configuration description

Property:

Data type:

Description:

Default:

enable

string

enable disable memcaching

1

forceTimeoutToAllPages

int

set default memcache expiration time (has priority over TYPO3 setting)

0

disableCacheForLoogedUsers

int

enable/disable memcaching for logged users

0

cleanOnClearAllCache

int

if set, memcache is cleared when backend user click clear cache

setPageCacheTimeout OverrideAllTypoSettings

int

if you enable this option, you can override all expires time from your user_int extension, fg if you have page with four extensions and each extension calls static function: if (method_exists('tx_evo_nginx_boost', 'setPageCacheTimeout') tx_evo_nginx_boost::setPageCacheTimeout($timeout); evo_nginx_boost set expire time to the lowest value set in all 4 plugins

1

_mainServerIP

string

memcache server ip

_mainServerPort

string

memcache server port

_mainServerPersistent

int

enable persistant connection

1

_mainServerTimeout

string

memcache server connection timeout

1

memcacheSignature

int

add signature to end o pages

1

memcacheSignatureText

string

text of signature fg "CACHED BY ME :)". If you set memcacheSignature = 1 time of expiration is added to the end of memcacheSignatureText

1

excludedUrls

array

Set in ext_localconf. This is array of urls which should be excluded from memcaching. Let say you have confirmation message after successfuly saving post in user's blog. Page with message "your post was saved" will be memcached. You can add parameter message=1 and put this in array. Evo_nginx_boost exclude this url from memcaching.

1

The plugin is also equipped with a BE module, which allows us to monitor all parameters related to the memcache server. The cachedump option allows for viewing saved web pages and their assigned memcache keys:

 

Backend module

 

The solution was tested on a very complex Web site with a large number of USER and USER_INT extensions. Comments are suggestions are very welcome.

Download files:

Please note:

The versions of nginx available through apt-get in Ubuntu 8.04.1 exhibits an error.

Bugfix: a segmentation fault occurred in worker process, if the  “memcached_pass” and “if” directives were used in the same location.

If nginx and apache are run on separate machines, remember to match their date/time values. :)

Gravatar: Paweł Paweł, 26.03.2009 12:02
Witam,

literówka -&gt; przyspieszyć, nie przyśpieszyć


pzdr,
PKo
Gravatar: cheapcarsauctions cheapcarsauctions, 08.04.2009 19:13
Hello to all ! Great site. I am new here greetings to all from Poland.
Gravatar: georg kuehnberger georg kuehnberger, 10.04.2009 17:23
Bartosz, thanks for the interesting writeup;
I'd be interested in how the performance of your nginx-memcached setup compares
with a setup we're running using squid as reverse proxy and typo3s cache-headers. IMHO the squid setup is easier as it wont need memcached as squid keeps cached pages in memory and on disk. (memcached however would offer better scalability).
regards georg
Gravatar: Bartosz Aninowski Bartosz Aninowski, 10.04.2009 20:31
Hi Georg, I do not have lot of experience with squid. I can tell what you can do with nginx and memacache. You can very flexible controle what is memcached and how. You can memcache user_int extension. You can flexible clear memcache after various user actions. You can set different memcache timeout within your extension. With upcoming version you can clear memcache of page, tree, user (all pages where particular user make some actions). We developed this extension for community website where content change every miniute. We found that lot of very high traffic website use memcache. Please find very intresting article about memcache and facebook http://www.facebook.com/note.p....
Gravatar: EVO techblog » Zobacz co możes EVO techblog » Zobacz co możes, 15.04.2009 23:59
[...] Intensywnie testujemy nadchodzącą wersje evo_nginx_boost dla CMS TYPO3. Do czego służy i jak działa możecie przeczytać tu: Jak przyspieszyć działanie TYPO3 przy pomocy nginx’a. [...]
Gravatar: easfWWeebAsbnH easfWWeebAsbnH, 24.04.2009 05:11
07031.
Gravatar: Rainer Rainer, 07.05.2009 15:25
Hi Bartosz,

this sounds very interesting.
What version of Typo3 is required for this extension?


Best Regards,
Rainer
Gravatar: Bartosz Aninowski Bartosz Aninowski, 07.05.2009 15:28
4.2.X and higher
We are going to publish new version next week.
New version will work in two modes: with nginx and standalone (with small odifications of index.php)
Gravatar: Rainer Rainer, 07.05.2009 15:55
Ah, OK.

Thanks for the info!
Gravatar: Bartosz Aninowski Bartosz Aninowski, 15.05.2009 14:09
Hi Rainer :)
I try to upload new version till sunday. (till monday, edited on sunday :))
Gravatar: Nick Weisser Nick Weisser, 18.05.2009 23:08
I'm using Nginx on one of our servers for Magento, but recently thought about using it for TYPO3, too.

Will try this out anytime soon. Thanks for the nice write-up!
Gravatar: Bartosz Aninowski Bartosz Aninowski, 18.05.2009 23:10
There is a new version available. I will add new manual tomorrow. We've added lots of new features.
Gravatar: Ironman Ironman, 21.05.2009 20:59
I wonder why you are still using Apache and do not run typo3 directly on nginx...any reason for that?
Gravatar: Bartosz Aninowski Bartosz Aninowski, 21.05.2009 21:09
That's depends on your architecture. I use nginx with 3 backend server, so Nginx works like load balancer. We had previous setup with apache so it was easer for us to add nginx as frontend server. I have also lower load with apache+mod_php5 than nginx+fastcgi or apacha+fastcgi. Anyway you can use nginx+memcache+fastcgi if you want.
Gravatar: Markus Bischof Markus Bischof, 04.06.2009 11:33
First of all thank you for this really interesting article. I'm running a lot of Typo3-Sites on my server. Is there a way to have one nginx.conf which work for all my sites or is one config for each Typo3-Site a must have?
Gravatar: Bartosz Aninowski Bartosz Aninowski, 04.06.2009 13:01
You can have one config for all your site. That is no problem.
Gravatar: Markus Bischof Markus Bischof, 04.06.2009 13:46
Maybe you have some hints for me? I've configured nginx, memcached and apache so far, that one page works find. What must be done to have your config working for all my domains? Are there some kind of variables for the domainname? I'm a little bit confused at the moment.
Gravatar: Alexander Bohndorf Alexander Bohndorf, 02.07.2009 12:20
I wrote an extension to add auto clearing of the cache if BE users make any changes on pages or content elements. If you are interested in embedding this functionality in your extension then please contact me. It is done with hooks. Additionally I found a small bug in the default mode of the module. Thanks for your great work. It is milestone for TYPO3 performance and quite easy to configure!
Gravatar: Bartosz Aninowski Bartosz Aninowski, 02.07.2009 16:33
Hi Alexander. Thanks for comments.
Can you open bug report on Forge ?
http://forge.typo3.org/project...
Gravatar: EVO techblog » Evo_nginx_boost EVO techblog » Evo_nginx_boost, 19.07.2009 19:45
[...] Jak przyspieszyć działanie TYPO3 przy pomocy nginx’a. [...]
Gravatar: Veit Nachtmann Veit Nachtmann, 17.08.2009 18:03
Hi, I made a patch that fixes issues with php5-memcache-3.x (e.g. debian lenny)

--- class.tx_evo_nginx_boost.php.old 2009-08-12 06:58:55.000000000 +0200
+++ class.tx_evo_nginx_boost.php 2009-08-12 07:05:00.000000000 +0200
@@ -239,7 +239,19 @@
*/
public function getServersStatistics($type = null, $slabid = null, $limit = 100)
{
- return $this-&gt;connected&gt;0 ? $this-&gt;memcache-&gt;getExtendedStats($type, $slabid, $limit) : false;
+ /* patch (c) 2009 by Veit Nachtmann, veit@nachtmann.it
+ *
+ * $type cannot be null or the lib (&gt;3.x) will throw an error. simple workaround.
+ */
+ if ($this-&gt;connected &gt; 0) {
+ if (is_null($type)) {
+ return $this-&gt;memcache-&gt;getExtendedStats();
+ } else {
+ return $this-&gt;memcache-&gt;getExtendedStats($type, $slabid, $limit);
+ }
+ } else {
+ return false;
+ }
}

/**
Gravatar: bart bart, 17.08.2009 20:58
Thanks for the path.
We will put it soon.
Gravatar: Veit Nachtmann Veit Nachtmann, 20.08.2009 16:55
I'm having trouble with this, everything misses the memcache (using nginx), and with the php-approach, every page stays blank...
any hints?
Gravatar: bart bart, 20.08.2009 16:58
Do you have any record in CacheDump section?
Gravatar: Feelx Feelx, 21.08.2009 18:07
Hi, great work!
It's very interesting and I tried nginx to serve the TYPO3 rendered pages. It gives me 50% more speed (while no load). Parsing time is about 35ms instead of 70ms.
But what I wonder is what port to you bind apache to? You write nothing about that, and is there no conflict, if apache and nginx listen to Port80?
Thank you
Greetings
Feelx
Gravatar: bart bart, 21.08.2009 18:32
You can setup apache to listen on 127.0.0.1 port 82 and proxy nginx to mentioned port. Nginx is great tool to make load ballancer.
I have big doubts if you can reduce time by 50% with nginx+php. Plase check this benchmark http://blog.a2o.si/2009/06/24/...
Nginx+php+memcached this is the best way:)
Gravatar: Veit Nachtmann Veit Nachtmann, 24.08.2009 16:32
hey, nevermind, I fixed it.
I was using the wrong cache-key with nginx -.-, php failed for some other reason, now it works perfectly.
I also use nginx to punch out static files, works IMHO much better than relying on apache to do the job (I have both on the same machine).
Thanks you your work!
Gravatar: bart bart, 24.08.2009 18:37
Great that you like and it and fix your problem :)
Gravatar: Veit Nachtmann Veit Nachtmann, 02.09.2009 17:13
hi,

after deploying it in our live-environment, I had to deactivate it again, because it slightly uses more CPU than bare typo3, so the load (which is at the edge right now), increased to 120 :(
The problem are the cache-misses, it's like 1:10, hit:miss. With 200 objects in the memcache. 30000 misses after 1 hour.

How could I debug that?

I guess this is a problem with cookies, aparently 90% of our users have them set, though I'm not sure. Tests with ab work perfectly, 2kreq/s at least. But on the live env, this just explodes ;)
plz halp :D
Gravatar: bart bart, 02.09.2009 17:32
nginx or php-aproach?
Gravatar: bart bart, 02.09.2009 17:40
what you mean "...with cookies, aparently 90% of our users have them set,"?
These are logged users? If yes, please turn off memcaching for logged users and check again. I need more info about your configuration.
Gravatar: Tomaz Zaman Tomaz Zaman, 07.09.2009 15:37
Is there an option that USER_INT object would not be cached AT ALL? I have some plugins on my webpage that display user's information based on IP (in footer, so basically every page has a USER_INT objact).
Gravatar: bart bart, 07.09.2009 15:56
No, ajax-based solution is your friend :)
Gravatar: louS louS, 07.09.2009 18:34
Hello,
i wanted to try this nice extension. But when i try i get an error on my FE, it says that the tx_evo_nginx_boost_index_conf.php is not there.

I wrote in my index.php:

require_once('typo3conf/ext/evo_nginx_boost/tx_evonginxboost_index.php');

And then it says on line 6, the file is not there and this is the conf. file.

What should i do?

P.S.: I dont got NGINX server on my server.
Gravatar: louS louS, 07.09.2009 19:35
Hmm i did, now i get this error:

Fatal error: Class 'Memcache' not found in /typo3conf/ext/evo_nginx_boost/class.tx_evo_nginx_boost.php on line 17
Gravatar: louS louS, 07.09.2009 19:46
Ah ok ;D

Its hard to install on my server?
Gravatar: louS louS, 07.09.2009 19:50
Ok sure no prob :D

Do i have to do something else after i installed php memcache?
Gravatar: louS louS, 07.09.2009 19:52
Ah i forgot to tell you, when i click on "Evo Nginx Boost" in my typo3 menu, i just see a blank site :D
Gravatar: Bartosz Aninowski Bartosz Aninowski, 07.09.2009 19:55
1. install memcached server
2. install php-memcache
3. restart web serwer
4. install evo_nginx_boost
5. add require_once to index.php
6. set extendTypo3indexphp to 1 and update configuration
7. seat and watch :)
p.s if something go wrong, check webserver error log,
Gravatar: louS louS, 07.09.2009 20:00
memcached server?

You sad i just need to install php memcache? :D
Gravatar: louS louS, 07.09.2009 20:03
Ah never mind, i cant install php memcache :( I asked already
Gravatar: Bartosz Aninowski Bartosz Aninowski, 07.09.2009 20:04
memcached server and php-memcache
please use google :)
Gravatar: JOsh JOsh, 14.09.2009 22:56
I don't see the error reporting line in any index.php file. I see it in Typo3/init.php should I add it there? I have tried and I keep getting a fatal error:

Warning: Division by zero in /home/mosaics/public_html/typo3/init.php on line 69

Warning: require_once(php’) [function.require-once]: failed to open stream: No such file or directory in /home/mosaics/public_html/typo3/init.php on line 69

Fatal error: require_once() [function.require]: Failed opening required 'php’' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/mosaics/public_html/typo3/init.php on line 69
Gravatar: JOsh JOsh, 14.09.2009 23:09
Put the line in typo3/index.php at line 65 and it seem to work and is waayy fast!
Gravatar: bart bart, 14.09.2009 23:15
lin 65 is ok too, but better at top of index.php :)
I am glad that you like it. Regards.
Gravatar: ViruS ViruS, 16.09.2009 12:58
Witam,
ostatnio proboje przyspieszyc jedna strone ktora ma wg. opisu zrobiona ta konfiguracje, jednak zastanawiam sie dlaczego w katalogu typo3temp/cache_pages mam pare giga danych , a w memcached znacznie mniej.
Czy jest w ogole sens tego katalogu ?
da sie to jakos wylaczyc by siedzialo to tylko i wylacznie w memcached ?

oczywiscie mowie o typo3 4.2 , przy 4.3 to wiem jak to zrobic, jednak jest wciaz w wersji alpha.

Dziekuje z gory za odpowiedz.
Gravatar: bart bart, 16.09.2009 13:10
nie da się wyłączyć ponieważ nasze rozwiązanie nie jest zamiennikiem, jeśli w memcached skończy się miejsce które zaplanowałeś, wtyczka będzie nadpisywać rekordy przeterminowane. O ile dobrze pamiętam w wersji 4.2 typo3 nie posiada garbage collectora i nie zajmuje się usuwaniem wyexpirowanych rekordów. Możesz to zrobić sam pisząc odpowiedni moduł albo usuwać cały cache w nocy. Oczywiście zależy to od wielu czynników.
Gravatar: RAMOSBobbie29 RAMOSBobbie29, 21.04.2010 03:18
Different people all over the world get the
Gravatar: Andi Andi, 21.05.2010 19:09
Hi bart
Is nginx compatible with 4.4.0beta2+?
andi
Gravatar: Bart Bart, 21.05.2010 19:17
Yes It is.
Add comment

* - required field