Nginx+memcached Building a page cache

Source: Internet
Author: User
Tags memcached php script phpinfo

Traditionally,PHPused inmemcachemethod is to use thePhp-memcacheorphp-memachedExtended Operationsmemcache, inNginxThere are ways to build more efficient caching mechanisms. Is thatNginxDirect Accessmemcache, and useURIand theargswaitNginxbuilt-in variable setting cacheKeyrule, so that when the cache hits,Nginxcan skip throughfastcgiand thePHPthe process of communication, directly frommemcachegets the data and returns it. Memc-nginxand theSrcache-nginxIt is using this strategy to improve the efficiency of the cache.

When the cache is hit, Nginx can skip through fastcgi and the PHP the process of communication, directly from memcache gets the data and returns

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/97/12/wKioL1koymujiWfxAABHiNsXq5g722.png-wh_500x0-wm_ 3-wmp_4-s_3109675098.png "title=" 1.png "alt=" Wkiol1koymujiwfxaabhinsxq5g722.png-wh_50 "/>

Compile the installation nginx, the command is as follows:

./configure--prefix=/usr/local/nginx \

--add-module=. /memc-nginx-module\

--add-module=. /srcache-nginx-module\

--add-module=. /ngx_http_upstream_keepalive

Make

Make install

Then you need to configure Nginx, nginx default Master profile is placed under the Conf of the installation directory, the main configuration file is/usr/local/lnmp/nginx/conf/nginx.conf.

Related configurations

#Memcache Service Upstream

Upstream Memcache {

Server 172.25.80.1:11211;

}

server {

Listen 80;

server_name Server1;

#memc-nginx-module

LOCATION/MEMC {

Internal

Memc_connect_timeout 100ms;

Memc_send_timeout 100ms;

Memc_read_timeout 100ms;

Set $MEMC _key $query _string;

Set $MEMC _exptime 300;

Memc_pass memcache;

}

Location/{

root/var/www;

Index index.html index.htmindex.php;

}

#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

Location ~ \.php$ {

CharSet Utf-8;

Default_type text/html;

#srcache-nginx-module

Set $key $uri $args;

Srcache_fetch GET/MEMC $key;

Srcache_store PUT/MEMC $key;

root/var/www;

Fastcgi_pass 127.0.0.1:9000;

Fastcgi_index index.php;

Include Fastcgi_params;

Fastcgi_param Script_filename$document_root$fastcgi_script_name;

}

}

$MEMC _key The variable represents the key, which is directly using the Nginx built-in $query_string as key, $memc _exptime indicates the cache failure time, in seconds. This is set to 300 (5 minutes), and in practice you can set different expiration times for different content depending on the situation. Finally, the cache is configured for the location "~ \.php$", which means that all requests ending with ". PHP" will be cached .

The test is divided into three groups: the first group does not open the cache in Nginx and PHP, the second group uses only PHP memcache cache, and the third group uses only nginx memcache cache. All three groups use AB program to press, concurrent number is 20, request number is 10000.

Test:

Do not turn on caching, the script is as follows:

<?php

Phpinfo ();

?>

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/97/11/wKiom1koyx2SzA4_AAFDIy-m0IU720.png-wh_500x0-wm_ 3-wmp_4-s_4173451729.png "title=" 2.png "alt=" Wkiom1koyx2sza4_aafdiy-m0iu720.png-wh_50 "/>

The second group we use PHP to operate the cache, the test script is:

<?php

$MEMC = new Memcached;

$memc->addserver (' localhost ', 11211) Ordie (' Connect to Memcache server failed! ');

$output = $memc->get (' My_key ');

if (empty ($output)) {

Ob_start ();

Phpinfo ();

$output = Ob_get_contents ();

Ob_end_clean ();

$MEMC->set (' My_key ', $output, 300);

}

Echo $output;

?>

The test results are as follows:

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/97/12/wKioL1koy1uxLtYEAAGYwkyW1Zg982.png-wh_500x0-wm_ 3-wmp_4-s_1811011928.png "title=" 3.png "alt=" Wkiol1koy1uxltyeaagywkyw1zg982.png-wh_50 "/>

Finally, we return the PHP script to a version that does not use the cache, and configure the MEMC and Srcache caching mechanisms. The test results are as follows:

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/97/11/wKiom1koy52B_9_QAAEuhfxiuA4320.png-wh_500x0-wm_ 3-wmp_4-s_243166901.png "title=" 4.png "alt=" Wkiom1koy52b_9_qaaeuhfxiua4320.png-wh_50 "/>

The caching mechanism built using MEMC and Srcache on various indicators is much better than using PHP operations memcache. The number of processing requests per second (concurrency) and throughput is about 9 times times, while the average request takes only 1/8 of the traditional policy.


This article is from the "12444971" blog, please be sure to keep this source http://12454971.blog.51cto.com/12444971/1930062

Nginx+memcached Building the page cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.