首页 > 代码库 > openresty

openresty

Memc nginx+srcache nginx+memcached构建透明的动态页面缓存

一.原理

一种高效的缓存策略是Nginx直接访问memcache,并用$uri$argsNginx内置变量设定缓存key规则,这样,当缓存命中 时,Nginx可以跳过通过fastcgiPHP通信的过程,直接从memcache中获取数据并返回。memc-nginxsrcache- nginx正是利用这种策略提高了缓存的效率。下图是这种高效缓存策略的示意图(当memcache命中时)。

技术分享

Openresty/memc-nginx-module

二.配置安装

 

[root@server1 ~]# tar zxf openresty-1.11.2.3.tar.gz

[root@server1 ~]# tar zxf nginx_upstream_check_module-0.3.0.tar.gz

[root@server1 ~]# tar zxf ngx_cache_purge-2.3.tar.gz

[root@server1 ~]# cd openresty-1.11.2.3

[root@server1 openresty-1.11.2.3]# ./configure --prefix=/usr/local/openresty --with-http_realip_module --with-pcre --with-luajit --add-module=/root/ngx_cache_purge-2.3/ --add-module=/root/nginx_upstream_check_module-0.3.0/ -j2

 

 

  nginx http client request body temporary files: "client_body_temp"

  nginx http proxy temporary files: "proxy_temp"

  nginx http fastcgi temporary files: "fastcgi_temp"

  nginx http uwsgi temporary files: "uwsgi_temp"

  nginx http scgi temporary files: "scgi_temp"

 

cd ../..

Type the following commands to build and install:

    gmake

    gmake install

[root@server1 openresty-1.11.2.3]# gmake

[root@server1 openresty-1.11.2.3]# gmake install

mkdir -p /usr/local/openresty/site/lualib /usr/local/openresty/site/pod /usr/local/openresty/site/manifest

ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/bin/openresty

 

[root@server1 ~]# /usr/local/openresty/nginx/sbin/nginx -V    ##查看nginx版本,测试是否安装成功

nginx version: openresty/1.11.2.3

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.60 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.8 --add-module=../ngx_lua_upstream-0.06 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis2-nginx-module-0.14 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib --with-http_realip_module --with-pcre --add-module=/root/ngx_cache_purge-2.3 --add-module=/root/nginx_upstream_check_module-0.3.0 --with-http_ssl_module

 

 

[root@server1 bin]# nginx -t

nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful

 

[root@server1 bin]# nginx -s reload

 

技术分享 

 

 

 

三.编写配置文件

[root@server1 memc-nginx-module-0.18]# vim /usr/local/openresty/nginx/conf/nginx.conf

http {

upstream memcacheds {

        server 172.25.60.111:22222;

}

 

 

server  {

        listen       8090;

        server_name  test.ttlsa.com;

        index index.html index.htm index.php;

        root  /data/wwwroot/www.ttlsa.com/webroot;

 

        location /memc {

                internal;

                memc_connect_timeout 100ms;

                memc_send_timeout 100ms;

                memc_read_timeout 100ms;

                set $memc_key $query_string;

                                                                                           

                set $memc_exptime 120;

                memc_pass memcacheds;

                }

 

        location ~ .*\.php?$

        {

                if ($uri ~ /ttlsa/){

                        set $ttlsa_key $request_uri;

                        srcache_fetch GET /memc $ttlsa_key;

                        srcache_store PUT /memc $ttlsa_key;

                        add_header X-Cached-From $srcache_fetch_status;

                        add_header X-Cached-Store $srcache_store_status;

                }

                include fastcgi_params;

                fastcgi_pass  127.0.0.1:10081;

                fastcgi_index index.php;

                fastcgi_connect_timeout 60;

                fastcgi_send_timeout 180;

                fastcgi_read_timeout 180;

                fastcgi_buffer_size 128k;

                fastcgi_buffers 4 256k;

                fastcgi_busy_buffers_size 256k;

                fastcgi_temp_file_write_size 256k;

                                                                                                   fastcgi_intercept_errors on;

                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        }

}

 

}

四.测试

 

[root@server1 ~]# curl -I 172.25.60.111

HTTP/1.1 404 westos cache

Server: Varnish

Content-Type: text/html; charset=utf-8

Retry-After: 5

Content-Length: 398

Accept-Ranges: bytes

Date: Fri, 26 May 2017 16:52:09 GMT

X-Varnish: 2131058511

Age: 0

Via: 1.1 varnish

Connection: close

 

openresty